-1

When I press back in my activity , it calls the onSaveInstanceState() and saves the "bundle savedInstanceState" which is right. But I want to destroy it or set it to null only when I press back button or on destroying activity

I just need onSaveInstanceState() inside my activity for configuration changes.

I think I need to use onBackPressed() but what should I put in it ???

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Edit :

Lets say I have an activity contains a list when I click an item on the list the activity I mentioned above will start.

My second activity is a master/detail flow activity, When I click an item on the list of this activity it will update the detail's fragment (which is a viewpager here ) and it will save the current choice of the list.

Now when I press back button on second activity, it goes back to the list activity, again if I click a list item it will goes to second activity with the choice I had before pressing back. ( it's kinda save the choice through back stack )

But I noticed it has nothing to do with savedinstancestate, My second activity will not destroy through back button it will detach and then attach like fragments.

I want to completely destroy second activity when the back button is pressed.

Edit 2 :

When i even close my application through back button and launch it again, and then go to second activity, I see my last state and the item I clicked. ( I used setChoiceMod(...single) on the list )

Thanks for your help.

Saeed.re
  • 740
  • 9
  • 19
  • What are you trying to achieve exactly? Are you trying to go back to an activity that it not in order of the backstack? You can clear the backstack and start the last activity again. Otherwise stick to the guidelines and let the users use the back button how it's intended. – RED_ Feb 23 '14 at 11:37
  • @RED_ I updated my post, check it now ... – Saeed.re Feb 23 '14 at 12:10
  • You can override the onBackPressed() method and add this.finish(); – John J Smith Feb 23 '14 at 12:30
  • @JohnJSmith, It didn't worked, It loads my last state that i was in second activity again. – Saeed.re Feb 23 '14 at 12:49
  • @JohnJSmith overriding `onBackPressed()` to call `finish()` is not necessary, nor does it change anything. The default implementation does exactly that! – David Wasser Feb 23 '14 at 12:54

2 Answers2

0

It should be this way:

@Override
public void onBackPressed(View v){

onBackPressed();
}
});
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
0

I noticed that the variables I defined in the second activity, is saving their values (that I changed in activity) through back stack and even closing application wouldn't delete them.

The variables :

static int mcurchoice ;
static int mcurtab ;

I solved my problem by setting them to 0 in onBackPressed() method :

mcurchoice = 0;
mcurtab = 0; 

can someone explain in comments how they will save their values through activity's destroy .?

Saeed.re
  • 740
  • 9
  • 19