Daniel
Daniel

Reputation: 97

Android: Retain values of previous activity

I have a main activity in which there is a spinner and a button. Spinner contains four items: Item 1, Item 2, Item 3, Item 4. When this activity is loaded, by default Item 1 is selected. On the click of the button, new activity (activity2) is called like this:

Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);

Activity2 has a BACK button in the action bar like this:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Now, suppose I select Item 3 on main activity and hit the button. Activity 2 opens up. Now when I hit BACK button in action bar, main activity opens up but the Item 3 is not retained in the spinner. Spinner contain Item 1.

When I debugged, I found out that onCreate method of main activity is called when the BACK button of activity 2 was pressed. It means my main activity is getting killed.

Question: How can I retain Item 3 in the spinner in main activity?

Upvotes: 1

Views: 1833

Answers (3)

Daniel
Daniel

Reputation: 97

As per @PavneetSingh comment, I set launchMode to singleTop for the MainActivity in the AndroidManifest.xml file like this:

<activity android:name=".MainActivity"
        android:launchMode="singleTop">
</activity>

It worked. Now when I select any value in the spinner, press the button and move to the next activity, press the back button in that activity to come again to the main activity, the value of spinner is retained. That is what I exactly needed.

Upvotes: 7

Carlos Sifuentes
Carlos Sifuentes

Reputation: 101

You can save and restore your instance in onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) or onCreate(Bundle). Android will typically destroy your previous Activity to save resources but provides these callbacks to save important data to restore the state when needed. You can find more information on https://developer.android.com/guide/components/activities/activity-lifecycle.html under the section "Lifecycle callbacks".

Upvotes: 0

Waclock
Waclock

Reputation: 1716

You can pass values from one activity to another using a bundle:

Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);

Then on your other activity

Intent intent = getIntent();
if (null != intent) { //Null Checking
  String StrData= intent.getStringExtra(KEY);
  int NoOfData = intent.getIntExtra(KEY, defaultValue);
  boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
  char charData = intent.getCharExtra(KEY, defaultValue); 
}

EDIT:

If you want the to retain the values when "going back", what you should do is start the activity without finishing the current activity. This will add the new activity to the stack. So let's see this in an ordered manner:

  1. Your spinner is in Activity1
  2. User sets the spinner with information. You start Activity2 without finishing Activity1
  3. User does something in Activity2; and clicks in a button, triggering to go back to Activity1. To go back to Activity1 you should just finishActivity()in Activity2. This will bring Activity1 with its state back to screen.

Since you're using the DisplayHomeAsEnabled, your code should look like this when ending the activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
       finish();
    }

    return super.onOptionsItemSelected(item);
}

Upvotes: 0

Related Questions