Finish parent and current activity in Android

前端 未结 16 3123
误落风尘
误落风尘 2020-11-27 03:40

I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should

16条回答
  •  旧时难觅i
    2020-11-27 04:20

    I didn't do any of this. This is how I would revise your code.

    the code you use to enter another intent:

    Intent whatEverIntentName = new Intent("Path.to.next.Intent");
    startActivity(whatEverIntentName);
    finish();
    

    This way, you always quit when pressing back. But wait! You can change how you want your back key press to react when pressed.

    Do this:

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent whatEverIntentName = new Intent("Path.to.the.activity.before.it");
        startActivity(whatEverIntentName);
    // Don't add finish here. 
    //This is necessary because you finished your last activity with finish();
    }
    

提交回复
热议问题