简体   繁体   中英

Maintain Activity state when changing to a different Activity

I have the following scenario:

  1. Activity A > Activity B
  2. Activity A < Activity B

What i would like to do is keep the state of Activity A when clicking Activity B's back button.

Activity A code:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
    {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    // check for saved instance
    if (savedInstanceState != null)
    {
        //restore saved values
    } 
    else
    {
        //initialize members with default values
    }
}


@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    savedInstanceState.putString("typeID", typeID);

    super.onSaveInstanceState(savedInstanceState);
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);

    typeID = savedInstanceState.getString("typeID");
}

public boolean gotoActivityB(View view)
{
    Intent intent = new Intent(getApplicationContext(), ActivityB.class);
    startActivity(intent);

    return false;
}

Activity B code:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy_item);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
    {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    //do some magic...
}

public boolean onOptionsItemSelected(MenuItem item)
{
    Intent intent = new Intent(getApplicationContext(), ActivityA.class);
    startActivity(intent);

    return false;
}

Every time i go from Activity B to Activity A, savedInstanceState is equal to null, in other words, Activity A state isn't saved or restored.

What am i missing here?

!!SOLUTION!!

Based on @cybersam 's answer, Activities maintain their state by default. So there is no need for the savedInstanceState. To solve my problem i only had to update my back button events to:

public boolean onOptionsItemSelected(MenuItem item)
{
    finish();

    return true;
}

As the documentation for onSaveInstanceState() states:

An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

So you cannot assume that onSaveInstanceState() would be called on A just because B is launched in front of it. In fact, most of the time, it will not be.

[EDITED]

Your code for B seems to be calling startActivity() to "go back" to the prior activity. If you just want B to go back to the prior activity, you can (usually) just call finish() to exit B, which should allow A to reappear (with its state intact), since it will become the top Activity in the stack .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM