Close all running activities in an android application?

后端 未结 11 1731
死守一世寂寞
死守一世寂寞 2020-12-11 19:19

I create one application and never use finish() for each activity. If my user clicks on the logout button it goes to the previous page.

How can I close my previous

相关标签:
11条回答
  • 2020-12-11 19:30

    This is one workaround that i have tried and it worked for me perfectly.

    SOLUTION-1

    this.finish();//try activityname.finish instead of this
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

    use this in the activity where you want to exit from your application....

    ================================================================================== Above code helps to resume your app where you last left off.

    SOLUTION-2

    If you want to exit from the application and also close all running activities you need to use.

    onActivityResult()
    

    For eg- suppose there are 3 activities A,B,C you navigate from A->B->C and at C you want to close all activities then use following sample.

    Activity A

    public class A extends Activity {
    int Finish = 100;
    Button launch_second;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        launch_second = (Button) findViewById(R.id.start_second_act);
        launch_second.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Intent second = new Intent(A.this,
                        B.class);
                startActivityForResult(second, Finish);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 100:
            this.finish();
            break;
        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    
    }
    

    Activity B

    public class B extends Activity {
    private Button launch_next;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_main);
        launch_next = (Button) findViewById(R.id.start_third_activity);
        launch_next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent third = new Intent(B.this,C.class);
                startActivityForResult(third, 100);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 100:
            setResult(requestCode);
            this.finish();
            break;
    
        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    
    }
    

    Activity C

    public class C extends Activity {
    Button kill_app;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_main);
        kill_app = (Button)findViewById(R.id.kill_app_btn);
        kill_app.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                C.this.finish();
                setResult(100);
    
            }
        });
    
    }
    
    }
    

    SOLUTION-3

    There is method available finishAffinity() call it from any activity,all previous activities will get destroyed.

    0 讨论(0)
  • 2020-12-11 19:31
    @Override
        public void onClick(View v) {
    
            if (v == btnAtGlanceBack) {
                Intent backIntent = new Intent(this, Dashboard.class);
                startActivity(backIntent);
    
            } else if (v == btnAtGlanceLogOut) {
                Intent logoutIntent = new Intent(this, GlobalScholarLogin.class);
                logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(logoutIntent);
    
            }
        }
    
    0 讨论(0)
  • 2020-12-11 19:34

    In general android main stack for all you activity your launching and remove it based on the way you call to next activity. If you want to clear all activity set this flag Intent.FLAG_ACTIVITY_CLEAR_TASK while launching activity in order to clean up previous activity.

    0 讨论(0)
  • 2020-12-11 19:43

    EDIT

    Sam Janz answer is cleaner than this method. Use the intent flags to your advantage

    When the user presses log out:

    Intent intent = new Intent(this,MyHomeActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putBooleanExtra("finishApplication", true);
    startActivity(intent);
    

    Then in MyHomeActivity (Your start activity) in onResume:

    if(getIntent().getBooleanExtra("finishApplication", false){
       finish();
    }
    

    This way you don't have to have checks in all your activity's only the Home activity.


    Dirtier option:

    Create a static boolean variable in a singleton somewhere (probably in a class that extends application);

    public static boolean loggingOut = false;
    

    When the user presses log out set this to true and call finish on that activity.

    YourApplication.loggingOut = true;
    finish();
    

    In each activity in onResume()

    if(loggingOut){
       finish();
    }
    

    Ensure you set this boolean back to false in your main/start up activity:

    if(loggingOut){
       finish();
       YourApplication.loggingOut = false;
    }
    

    If you also want the back button to do it, override onBackPressed(), that would then also do

    @Override
    public void onBackPressed(){
         YourApplication.loggingOut = true;
         super.onBackPressed();
    }
    
    0 讨论(0)
  • 2020-12-11 19:44

    To clear whole stack which is tracking your activities in android you can use following code

    Intent intent = new Intent(this, MyHomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            finish();
            startActivity(intent);
    
    0 讨论(0)
  • 2020-12-11 19:46

    Thanks for your replies. I solved my problem.

    Solution: I write a file with some data and when the user clicks logout, I remove data from that file and finish() current activity, and all in previous activity I write code in onResume() I read file and if it is blank or null finish() so in that it will close all your activity and your application get close. Thank you for the great help.

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