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();
}
Related
My problem is that I have an arraylist of arraylists with custom objects that needs to be passed from one activity to another.
Clearly, let's say I have something like:
ArrayList<ArrayList<Statement>> data;
in one activity and I want to pass it to another. So, first what I have done is to make Statement implement the Parcelable class.
Then in the first activity (sender) I call the putExtra() method by passing it the data.
In the second activity (receiver) I call the getSerializableExtra() method to get data.
That works. But I have read that Parcelable would be better for efficiency etc. and so I tried to call putParcelableArrayListExtra() in the sender activity and the getParcelableArrayListExtra() method on the receiver activity.
But when I do that, I get red underlines indicating sth. like
ArrayList< android.os.Parcelable > is required
in the first activity which is the sender I have the line:
Intent intent = new Intent(SenderActivity.this, ReceiverActivity.class);
intent.putParcelableArrayListExtra(SenderActivity.EXTRA_LISTOFSTATEMENTLISTS, dataListOfStatementLists);
In the receiver activity I have sth. like:
myList = ( ArrayList<ArrayList<Statement>>) getIntent().getParcelableArrayListExtra(EXTRA_LISTOFSTATEMENTLISTS);
What I need to fix ? I know the basics about how to send Parcelable objects from one activity to another. But that was all things like
ArrayList<ParcelableObject> data
I have never done it for nested data like this
ArrayList<ArrayList<ParcelableObject>>
I hope someone can help.
Thanks in advance
This is just another way to do it. I suppose you know how to get the values in other activity.
ArrayList<ArrayList<Statement>> data;
Bundle bundle = new Bundle();
bundle.putInt("size", data.size());
for (int i = 0; i < data.size(); i++) {
bundle.putParcelableArrayList("item"+i, data.get(i));
}
Try this to send Data
Intent intent = new Intent(SenderActivity.this, ReceiverActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("object", List);
in.putExtra("list", bundle);
startActivity(in);
For Recieving Data
Intent i=getIntent();
Bundle bundle= i.getBundleExtra("list");
ArrayList<Statement> list = bundle.getParcelable("object");
Every time you want to pass data from one activity to another one via Intent you have to make sure that the classes where you create the objects that you want to send are implementing Serializable, otherwise it won't work. Your problem could be something related to this so I recommend you to implement Serializable on your classes.
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.
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.
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());
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.