how to finish all activities and close the application in android?

前端 未结 9 1098
自闭症患者
自闭症患者 2021-01-03 19:53

My application has the following flow:

Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3

My problem is that when I am try

相关标签:
9条回答
  • 2021-01-03 20:11

    Sometime finish() not working

    I have solved that issue with

    finishAffinity()

    Do not use

    System.exit(0);
    

    It will finish app without annimation.

    0 讨论(0)
  • 2021-01-03 20:13

    Use finishAffinity() method that will finish the current activity and all parent activities. But it works only for API 16+ mean Android 4.1 or higher.

    API 16+ use:

    finishAffinity();
    

    Below API 16 use:

    ActivityCompat.finishAffinity(this); //with v4 support library
    

    To exit whole app:

    finishAffinity(); // Close all activites
    System.exit(0);  // Releasing resources
    
    0 讨论(0)
  • 2021-01-03 20:13

    You can try starting the Screen 3 with Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK

    0 讨论(0)
  • 2021-01-03 20:15

    To clear all the activities while opening new one then do the following:

    Intent intent = new Intent(getApplicationContext(), YourActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    
    0 讨论(0)
  • 2021-01-03 20:18

    Hi if you are in a fragment and are not able to use the finish method as it is(because finish should solve your problem) then you can use the getActivity.finish() method after startActivity(intent);. If you are not in a fragment you could directly use finish() after you startActivity(intent); line

    0 讨论(0)
  • 2021-01-03 20:27

    There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.

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