1

I'm trying to pass a custom object from one activity to another. The way this works is I have a ListView in my MainActivity that is empty when the app first starts. There is a button that leads to my RecordActivity. After the user fills out data they press a "done" button that passes the object back to my MainActivity and places it in the List. This works fine, however when I want to add a second object it just replaces the first instead of just adding it to the list. I'm assuming this is because I'm recreating my MainActivity everytime I call startActivity(intent). I need to save the state of my MainActivity and after searching I could not find a thread that solves my problem.

Here is the code in my RecordActivity to pass the object:

Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("newRecord", EXTRA_RECORD);
startActivity(intent);

and here is my code to retrieve this in my MainActivity:

Bundle extras = getIntent().getExtras();
if(extras != null){
    Record addedRecord = extras.getParcelable("newRecord");
    adapter.add(addedRecord);
}

with the onNewIntent method:

@Override
public void onNewIntent(Intent intent){
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Record addedRecord = extras.getParcelable("newRecord");
        recordArrayList.add(addedRecord);
        adapter.add(addedRecord);
    }

}

The adapter is my ArrayAdapter that populates the ListView

Edit:

I should note that I want this list to be persistent so that every time the app opens the list is already there from the last time. I don't know if this effects my problem since I will need to use a SharedPreference to store my list

Solution

In the onNewintent method:

Bundle extras = getIntent().getExtras();

should be:

Bundle extras = intent.getExtras();
5
  • Did you try adding android:launchMode="singleTop" in manifest for that activity? Jul 22, 2014 at 12:11
  • You can also add these values to an application class that stays alive for the live of the application. Here is the docs.
    – dsum27
    Jul 22, 2014 at 12:13
  • @MysticMagic I have not, do I add that to my MainActivity? I have never used the launchMode attribute before Jul 22, 2014 at 12:16
  • The activity for which you want to preserve state if its already started. Here I have explained how that works with a reference. See that Jul 22, 2014 at 12:17
  • @MysticMagic I added that and now my object is not being added at all. My RecordActivity is currently a child activity of my MainActivity, do I need to change that? Jul 22, 2014 at 12:28

2 Answers 2

1

Add below line while starting your MainActivity;

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Also onCreate method at MainActiviy is not going to be invoked after calling this. You have to handle extras at onNewIntent method.

Edit:

every time the app opens the list is already there from the last time

You must store the items of your ListView for this purpose at SharedPrefs or SQLLite DB.

6
  • I made that change and my ListView is no longer being populated at all Jul 22, 2014 at 12:39
  • As I mentioned above you must handle extras onNewIntent method. Did you?
    – Devrim
    Jul 22, 2014 at 13:56
  • I did, there may be a mistake in my code for that method, I made the edit in my original question Jul 22, 2014 at 13:58
  • I thought it would be invoked automatically, if that's not the case would I just do that in my onCreate method? Jul 22, 2014 at 14:04
  • Right now I have a log message that I'm using to debug in my onNewIntent method to check the size of my list. I excluded that log statement from my edit. That message does get printed Jul 22, 2014 at 14:07
0

Instead of starting the MainActivity in your RecordActivity, you can call finish() to return back to the main activity. When starting the RecordActivity start it:

Intent i = new Intent(this, RecordActivity.class);
startActivityForResult(i, EXTRA_RECORD_RESULT);

But before calling finish() you need to send the data back, you can achieve this by:

Intent resultIntent = new Intent();
resultIntent.putExtra("key", "value");
setResult(Activity.RESULT_OK, resultIntent);
finish();

And in your main activity this method gets called:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == EXTRA_RECORD_RESULT)
    {
        value = data.getStringExtra("key");         
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.