Ryan Sayles
Ryan Sayles

Reputation: 3441

Android preserve state of activity

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();

Upvotes: 1

Views: 110

Answers (2)

Ibrahim Yildirim
Ibrahim Yildirim

Reputation: 2771

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");         
    }
}

Upvotes: 0

Devrim
Devrim

Reputation: 15533

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.

Upvotes: 1

Related Questions