views:

83

answers:

2

I have a few dozen activities: 1-Main, 1-Animation, 10-Other's and it's sub-sets of more activities (couple of dozen).

From the Main, you can go to any of the Others via buttons.

The Others call sub-set activities and they call the Animation activity.

Each of the activities, including sub-sets and Animation, has a button to return to the Main activity.

All buttons take user to correct activity.

The issue: From the Main activity, I want to quit via using the device's Back key. But, what happens is that it steps backward through all the previous activities.

The "return to main" buttons (in each activity) has finish and onDestroy in it. So, I'm not sure why those screen/activities aren't destroyed...?

Any comments/suggestions/clarifications is appreciated - thanks

[adding code snippet]

Note: I moved/added/deleted the finish, onDestroy, onStop... tried many ways so what shows in the snippet is only one way I tried...

    //  ---------------------------------------------------------
mainMenu.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // do something
        Intent Maincode = new Intent();
          Maincode.setClassName(
                              "com.bt.test",
                              "com.bt.test.Maincode");
        //  startActivity(Maincode); // go to maincode  
          finish();
          onStop();
        onDestroy();
        startActivity(Maincode); // go to maincode  
    }
}); // end -----------------------------------------------
+2  A: 

Could you post your onClick handler for the return to main button?

You should be doing something like this:

Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();

Edit: You could also try setting this flag:

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This should clear the activity stack between the calling and receiving activity if the receiving activity is already in the stack somewhere.

danh32
thanks - code added
headscratch
danh32: Thanks - setting the flag seems to do it (not sure why finish...etc. didn't work as it works in my other codes, albeit they don't go several level deep into the stack.
headscratch
+1  A: 

First, you should generally never be calling onStop or onDestroy (or, for that matter, any of the other Activity lifecycle methods) yourself. Android will do that for you as a result of finish, and will probably get confused if you do it yourself.

Second, your "return to main" listener should not be calling any startActivity at all. Instead, if you want to clear the activity stack, you should just call finish there. If you might be several Activities away from main and want to return directly, you should use startActivityForResult when launching sub-activities, and set a result Intent with a true flag attached on 'main button'. Then, any intermediate activity will get onActivityResult called and if they see the flag they can immediately finish too so that control gets back to your main activity.

EDIT: actually, startActivity with FLAG_ACTIVITY_CLEAR_TOP is a much more straightforward way to get the same effect. Stick with that.

Walter Mundt
Walter: Thanks - I agree as I've noticed the app confusion in some cases. But, often I need to call it as it will call my defined method which contains things to release callbacks...etc.
headscratch
headscratch: why do YOU need to call it? Isn't it enough to know that it will get called anyway already when the activity exits?
Walter Mundt
Walter: In some cases, I have to (want to) kill the GPS, time-based functions and a few other things that without specifically killing them, they remain. Using the onDestroy to call my onDestroy method (which also contains super.onDestroy) is per the android dev site doc's.
headscratch