简体   繁体   中英

Return to an already created activity, or resuming

So I have set up a back button for an activity. It returns you to the menu screen. I want the original activity in which the back button was pressed to be resumed. However the code that I have, when the menu item is pressed instead of returning to the already created instance, it opens a new one. When the actual back button for the phone is pressed (the one that calls OnDestroy) it closes the multiple instances of all of the same activity. I can even see when closing them that the original created one with the saved variables is there still. Let me show you the code.

    public boolean onOptionsItemSelected(MenuItem item){
    Intent myIntent = new Intent(getApplicationContext(), Tools.class);
    startActivityForResult(myIntent, 0);
    return true;

This is the code for the actionbar back button in the original activity. It returns you to the tools class, which is an activity with buttons linking you to tools for the EMS app I am creating. The idea was to log times in the original activity, hit back, and go to Call Info where your times would be stated and saved from a global variable. Ok, so what if I want to go back to Tools, and click my vitals logger again, should return me to the page I left right? Nope.

        vitals = (Button) findViewById(R.id.vitals);
    vitals.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
            Class goVitals = Class.forName("com.vexos.emspal.Vitals");
            Intent goIntent = new Intent(Tools.this, goVitals);
            startActivity(goIntent);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
            finish();
        }

Now I call finish with this button in order to destroy the tools screen as it is just buttons and can be done away with, and can just open a new instance on hitting back. I know here somewhere, I should have something like an if statement to return me to a previously created instance of "com.vexos.emspal.Vitals". But upon clicking it again, it creates a new instance while the other hangs in the background waiting to me accessed again. How should I make this onClick method check if there is already a running instance, and then onResume it?

So what I understand, you have:

Activity A starts Activity B
Activity B starts Activity A
Activity B finishes itself.

If this is right, you now have two activities of class A, the one that was already started and the one you start with Activity B.

To fix this, don't start Activity A from Activity B, but just finish Activity B. So it should be:

 vitals = (Button) findViewById(R.id.vitals);
 vitals.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        finish();
    }

Based on the comment answer you gave me here is one of the possible solutions. You want the second activity to retain the same state after again clicking on the button. So you need to save its data and restore it.

Save all the data you need in the savedInstanceState bundle in the onSaveInstanceState which is called when the activity stops. Then get the data back in the onRestoreInstanceState or on the onCreate.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
   // Save the user's current game state
   savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
   savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

   // Always call the superclass so it can save the view hierarchy state
   super.onSaveInstanceState(savedInstanceState);
}

And then to recover the data:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

Please go here for more detail: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

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