Finish parent and current activity in Android

前端 未结 16 2977
误落风尘
误落风尘 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条回答
  • 2020-11-27 04:19

    try this

      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_HOME);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);                
    
    0 讨论(0)
  • 2020-11-27 04:19

    From the screen(B) you want to start the screen(C) with NO back screen, just start this screen (C) with:

    screenC.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
        | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(screenC);
    finish();
    

    The back in screenC will exit the application.

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2020-11-27 04:20
    Intent intent=new Intent(ActivityB.this, ActivityC.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    

    your activity C will the root activity and on press back will finish the apps.

    0 讨论(0)
提交回复
热议问题