Exit/Finish an app/activity - android

前端 未结 12 762
醉梦人生
醉梦人生 2021-02-05 05:04

I\'ve got 4 activities say Act1, Act2, Act3 and Act4. A button in Act1 opens Act2, a button in Act2 opens Act3, a button in A

相关标签:
12条回答
  • 2021-02-05 05:41

    If you have another activity behind ( in the application activity stack), you could use finish() to exit the current activity.

    However, the back function too suppose to remove the current activity from your application activity stack.

    In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.

    In your case:

    Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit 
    

    The above method will exit all the activities.

    However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.

    0 讨论(0)
  • 2021-02-05 05:49

    Use android:noHistory = "true" in your AndroidManifest.xml file

        <activity
            android:name=".Act1"
            android:noHistory="true" />
        <activity
            android:name=".Act2"
            android:noHistory="true" />
        <activity
            android:name=".Act3"
            android:noHistory="true" />
        <activity
            android:name=".Act4"
            android:noHistory="true" />
    
    0 讨论(0)
  • 2021-02-05 05:50
    Do a 
    <code>
    
    try
    {
    finalize();
    finish
    }
    catch(Exception error)
    {
    error.printStackTrace();
    }
    //This would exit your App neatly
    </code>
    
    0 讨论(0)
  • 2021-02-05 05:52
    Intent intent = new Intent(Act4.this, Act1.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

    Clear the flag before you switch back to previous activity. It might help you.

    0 讨论(0)
  • 2021-02-05 05:53

    Tray this one

    Intent intent =new Intent(MainActivity.this, ClassName.class);
      finish
    startActivity(intent);
    
    0 讨论(0)
  • 2021-02-05 05:55

    Check out this link:

    Click here

    You can use :

    @Override
    public void onBackPressed()
    {
         moveTaskToBack(true);
    }
    

    in all activities to close the app.

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