My Activity not reading data - android

Android 2.1 update 1
Eclipse 3.5
I have a problem reading data from my 2nd activity that is called from the 1st activity using intent. I have androidmanifest.xml setup correctly.
My First Activity has the following code:
Intent myIntent = new Intent(MainMenu.this, Testmenu.class);
myIntent.putExtra("com.tweaktool.MyAge",40);
myIntent.putExtra("com.tweaktool.Enabled", false);
startActivity(myIntent);
My 2nd Activity has the following code:
Bundle bun = getIntent().getExtras();
int myAge = bun.getInt("MyAge");
boolean enabled = bun.getBoolean("Enabled");
When I look at the above code in 2nd Activity it lists the following:
enabled = false
myAge = 0
Why is this doing this??? Am I doing something simple wrong??

You're putting data with one keys ("com.tweaktool.MyAge", "com.tweaktool.Enabled") and trying to get it with others ("MyAge", "Enabled") -- the bundle then just returns you defaults (0, false). To get what you've put, use the keys you've used.

Have your tried
int myAge = bun.getInt("com.tweaktool.MyAge");?

Related

Determine if data was sent via intent

Is there any way to determine if a specific boolean value was sent via an Intent? In other words, I know how to send data via an Intent and how to read it back, but I would like to know if data was sent.
The current getBooleanExtra method requires a default value, so I can't check if it wasn't sent by using this.
I currently have this:
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
If the Extras.EXTRA_SHOW_NAVIGATION_DRAWER value wasn't set at all, I'd like to do some extra work. Obviously if I get true it means it was sent, however if I get false there's no way to tell.
This can be done extracting the intent bundle:
Bundle b = getIntent().getExtras();
boolean hasNavDrawerSetting = b.containsKey(Extras.EXTRA_SHOW_NAVIGATION_DRAWER);
if (hasNavDrawerSetting) {
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
} else {
showNavigationDrawer = getResources().getBoolean(R.bool.hasSideMenu);
}
If you really want to check whether your value is coming or not then you can take help of Bundle object. You can pass your boolean value through a bundle object. If the bundle object is null then no value is received in the next activity. But if it is not null then you will surely receive the value of your boolean parameter only if you put it in the bundle. It may be true or false depending upon the value you set. Below I'm providing two code snippets. One is for calling activity and another is one for called activity.
Calling Activity -->
Intent intent = new Intent(this,Experimental.class);
Bundle b = new Bundle();
b.putBoolean("key",true);
startActivity(intent.putExtra("bundle",b));
Called Activity -->
setContentView(R.layout.experimental);
Bundle b = getIntent().getBundleExtra("bundle");
if(b != null){
Log.d("Value",String.valueOf(b.getBoolean("key",false)));
}
Hope I'm able to help you.

PutExtra in android - returning null in Fragment

I am having a problem with one of my arguments in putExtra.
I am declaring like this:
Intent upanel = new Intent(getApplicationContext(), MainActivity.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
upanel.putExtra("USER_ID",json_user.getString(KEY_UID));
upanel.putExtra("USER_FN",json_user.getString(KEY_FIRSTNAME));
upanel.putExtra("USER_LN",json_user.getString(KEY_LASTNAME));
upanel.putExtra("USER_PI", json_user.getString(KEY_PROFILEURL));
pDialog.dismiss();
startActivity(upanel);
and in my fragment I am reciving them like this
userID = this.getArguments().getString("USER_ID");
userFirstName = this.getArguments().getString("USER_FN");
userLastName = this.getArguments().getString("USER_LN");
userProfileURL = this.getArguments().getString("USER_PI");
userid, userfirstname and userlast name work fine, userProfileURL is always returning as null?
the value being sent is /var/www/image/1.jpg, the only thing I can think of is that it doesnt like the / characters? Is this likely and if so how do I escape this character?
Ok well this fixes it!
userprofileurl = getActivity().getIntent().getExtras().getString("USER_PI");
EDIT
Actually, I forgot to declare the put in my MainActivity, in my case the user process is loginactivity->mainactivity and the fragments are loaded from here, so initially I was setting them in my loginactivity and then not in my mainactivity to pass them to the fragment
a d'oh moment!

How to get results in new activity from the previous activity results in android?

I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}

Using integer from one class in another Android

I need some help with using integer from one activity to another.
I am making some basic math program(game). It gets two random numbers, random operator, and 30 secs to solve math problems as much as you can.
If you solve problem u get 1 point.
Anyway right now, I want to get number of points that user have made and use it in another activity called *RankActivity*.
Main activity is called *BrzoRacunanjeActivity* and it contains button and one *int* called *poenibrojanje* which get number of points that user have made, and when I click on button, it opens new Activity with this line:
startActivity(new Intent(this, RankActivity.class));
As you can see another Activity is called RankActivity, and there I wrote :
*BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity();*
*System.out.println("Number of points:" + a1.poenibrojanje);;*
and I get all time this reuslt: 09-22 09:09:14.940: INFO/System.out(289): Number of points:0
Try this:
Intent intent = new Intent(this, RankActivity.class);
intent.putExtra("points", pointsVar);
startActivity(intent);
In onCreate of RankActivity:
getIntent().getIntExtra("points", 0);
so you want to pass integer value from one activity to another activity? right...
try:
Intent intent = new Intent(currentclass.this, destination.class);
intent.putExtra("point", pointvalue);
startActivity(intent);
at destination activity:
final int getpoint = getIntent().getExtras().getInt("point");
This will solve your problem.
first of all make
static variable like as public static int poenibrojanje;
in your BrzoRacunanjeActivity.class file now you can use this variable in any other class like as
BrzoRacunanjeActivity.poenibrojanje
or you can use putExtras(); method.
in you main activity.
Intent i = new Intent(this, RankActivity.class);
i.putExtra("Value",poenibrojanje);
in your next activity
int v = (getIntent().getExtras().getInt("Value")) ;

Changing Screen / New Intent

I am trying to change screens from the first to second and back again, but with some extra variables passed. At the moment I go from 1 to 2, but when I click from 2 back to 1 it causes an error that the application stopped unexpectantly. I have issolated the problem to this part of the code. I'm not at the moment trying to pass any other variables with it just get it to change.
1st page package = max.multiplebuttons.com
Activity = multibuttons
1st page package = max.reason.com
Activity = reason_screen
public void nextquestion(){
Intent a = new Intent();
a.setClassName("max.reason.com", "max.multiplebuttons.com.multibuttons");
startActivity(a);
}
a.setClassName("max.reason.com", "max.multiplebuttons.com.multibuttons");
Your problem is here. The class multibuttons isn't inside the package max.reason.com.
Edit:
I can't give you the exact syntax because I need to see the naming conventions, but you have to do something like this:
Intent a = new Intent(Activity1.this, Activity2.class);
startActivity(a);

Categories

Resources