Android: Multiple Activities and OnDestroy Question

白昼怎懂夜的黑 提交于 2019-12-11 03:17:42

问题


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 -----------------------------------------------

回答1:


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.




回答2:


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.




回答3:


Finish does not just "finish" your application/activity, as the Documents state

"Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()."

In other words your call to finish will propagate back through any activity you had previously been in.



来源:https://stackoverflow.com/questions/3842150/android-multiple-activities-and-ondestroy-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!