Android Is An Intent Unique To An Activity or Global - java

I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?

Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.

information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.

Related

Use variables between two activities with different processes

I created my Base activity in separated process by adding to it's <activity> fragment in manifest:
android:process=":login_exception_process"
In Base activity I initiate my connection objects, and start next Activitty. But from another Activity when I try to get this instances of connection objects they are NULL.
Objects initiated by Base activity are saved in separated classes in static variables. How can I access this variable?
Passing values to another Activity
You have to start your next activity sending an extra in an Intent.
Intent intent = new Intent(this, NextActivity.class);
intent.putextra("keyName","value");
startActivity(intent);
And your next Activity, you can retrieve this value by doing this:
String data = getIntent().getExtras().getString("keyName");
Here is the documentation for that.
Getting a result from an Activity
Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. For example, if you want to start the NextActivity and return a value back from NextActivity to BaseActivity, you can use startActivityForResult().
Here is the documentation for that.
Update
Sending objects via intent can be done with two options:
Pseudo-codes
Serializable
intent.putExtra("MyClass", your_object);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Parcelable
Intent mIntent = new Intent(this, NextActivity.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, your_object);
mIntent.putExtras(mBundle);
startActivity(mIntent);
Despite Serializable seems to be the easiest option, follow this tutorial it will teach you both ways.
Hope this helps you.
You should use Extras
How do I get extra data from intent on Android?
Basically, when creating your intent you have to put extras:
intent.putExtra("name", "My name is John");
then when you want to get it back when you start the next activity:
this.getIntent().getExtras().getString("name");
You can do this with any type if you convert the Object to a byte array.

Use the same object between activities

I have tried a lot of different ways to use the same object in different classes. I want to do this because I want the data to be stored in this object.
But no matter what I do, it hust wont be stored when I press the "back" button on the phone and then go back.
I am sending a bundle with the object to the second activity like this:
Intent intent = new Intent(this, TrainingDays.class);
Bundle bundle = new Bundle();
bundle.putSerializable("programObject", program);
intent.putExtras(bundle);
startActivity(intent);
And I am receiving the object like this:
Bundle bundle = this.getIntent().getExtras();
program = (Program) bundle.getSerializable("programObject");
I can use the object just fine, but I every time I switch back to another activity, the object is clean from data. Am I doing something wrong here or could it be something else that causes this? Any help will be greatly appreciated.
That's the correct way to manage objects. If you do not need to write the object on a File I would suggest to switch to Parcelable. About your problem, if you want to pass back the object, you can use the pair startActivityForResult and onActivityResult
Override the onBackPressed() method of Activity and use #blackbelt 's combination of startActivityForResult() and onActivityResult()
You can see an example on the developer page http://developer.android.com/reference/android/app/Activity.html#StartingActivities
onBackPressed() controls going back to the previous activity you could create a new Intent in the onBackPressed(), then a create a Bundle and Put the Bundle in the setResult() method as such setResult(RESULT_OK, mBundle); and Bundle Object will get passed back the previous activity.
in Short
#Override
public void onBackPressed(){
Intent data = new Intent();
Bundle newBundle = new Bundle();
newBundle.putParcelable("MyObject", mObject);// must extend Parcelable on Object class
data.putBundleExtra("ObjectBundle", newBundle);
setResult(RESULT_OK, data);
finish();
}

intent flag over intent startActivity(i);

#Override
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), Profile.class);
startActivity(i);
}
});
what is the difference of using this code and what difference does it do on the program compared to this doe
Intent i = new Intent(CurrentActivityName.this, NextActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
First one uses getApplicationContext() to launch the intent.
Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.
Second one uses the Activity context.
Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack).
So, Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.
Source: this answer at difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this question.
Also, reading some of above links would help:
What's the difference between the various methods to get a Context?
getApplicationContext(), getBaseContext(), getApplication(), getParent()

How to save an intent from one class to another class

I am new to Android so apologies if I am asking something silly. I am trying to develop an alarm clock application - basically, it's my final project and I am trying to develop an alarm like there is in API level 2.3.3.
I have designed the list view that takes input through a dialog box like time. I have also coded it to set an alarm.
Now I want to save that alarm as an intent in the other class, and I don't have any idea how to save different alarms in the other activity. I have also checked for the desk-clock alarm code but I didn't get that too.
Please help me someone, I am stuck here for the code for more than a week. Please someone help me, I shall be thankful to you.
If you want to send an Intent from one Activity to another, and then retrieve information from inside the Intent, the best way is use the Bundle object inside the intent:
Let's supose you send the intent from Activity1 to Activity2...
In Activity 1:
Intent intent = new Intent(Activity1.class,Activity2.class);
//I use the String class name as a key value, but you can use whatever key
intent.putExtra(String.class.getCanonicalName(), myString);
startActivity(intent);
//Or this other method if you want to retrieve a result from Activity2
//startActivityForResult(intent,Activity2);
In Activity 2:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String myString = bundle.getString(String.class.getCanonicalName());

Call other activities in an activity?

Say I have 2 activities (ActivityOne and ActivityTwo). How would I call ActivityTwo from ActivityOne? Then how would I return to ActivityOne from ActivityTwo? For example, I have a listview with all the contacts on the host phone. When I tap on a contact, another activity shows information and allows editing of that contact. Then I could hit the back button, and I would go back to the exact state that ActivityOne was in before I called ActivityTwo. I was thinking an Intent object, but I am not sure. Could someone post some code?
In your first activity (AcitvityOne) you could call
Intent intent = new Intent(this, ActivityTwo.class);
startActivityForResult(intent, CODE);
Then you can override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
to receive the result of this activity
In your example, it seems like you will want to pass data between Activity One and Activity Two (namely, the reference of the contact you want to edit), so you should use a Bundle with your intent in Activity One.
Intent myIntent = new Intent(this, ActivityTwo.class);
myIntent.putExtra("CONTACT", position);
this.startActivity(myIntent);
"CONTACT" is a key and position is a value (position would have been defined before, it could be whatever you want it to be, for example an integer. The way you retrieve it is slightly different based on what type it is, note the getInt() in Activity Two code below).
And in Activity Two, in onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
contact = extras.getInt("CONTACT");
/**Do what you want with the integer value you have retrieved**/
}
As for your other question, the back button removes the current Activity from the top of the tasks stack and therefore, you return to the previous Activity, normally in exactly the same state as you have left it.
However, if the phone is running low on memory, Android might kill off a process or Activity so it is possible in theory that your previous Activity will not show in exactly the same state as you have left it. If you have any important data, you should save it before launching Activity Two. If you just want to return to Activity One in the same state for user friendliness, then it's fine to rely on the back button.

Categories