Calling finish() removes the activity from activity stack?

廉价感情. 提交于 2019-12-08 12:43:49

问题


I have made an application where I

  1. start activity 'Pin' after I start activity A(activity A has noHistory= true, so it will not stay on stack).
  2. If no pin has been set up; PinActivity also starts an activity NewPinActivity.
  3. After setting pin, NewPinActivity calls finish() and goes back to PinActivity. Where you will give appropriate pin, it will call finish() on it and it will move again to activity A(Since activity A had noHistory=true, so I have to start a new instance of activity A). My problem is, if I press back from activity A, sometimes my NewPinActivity shows up again even if I called finish() on it.

回答1:


No, you should use in a second activity:

        Intent exitIntent = new Intent(this,MainActivity.class);
        exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(exitIntent);
        MainActivity.exitHandler.sendEmptyMessage(0);

and in MainActivity:

    exitHandler = new Handler() {
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
        case 0:
            // clear any informations you like here
            MainActivity.this.finish();
            break;
        }
    }
};



回答2:


Sounds like you should be starting NewPinActivity using startActivityForResult. On a successful result PinActivity should launch ActivityA with Intent.FLAG_NEW_INTENT and finish(); itself? Curiously why are you starting your ActivityA with noHistory?



来源:https://stackoverflow.com/questions/25057003/calling-finish-removes-the-activity-from-activity-stack

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