Clear Activity Stack and start new activity in android

后端 未结 4 900
有刺的猬
有刺的猬 2020-12-11 02:13

My question is little bit different than these type of question. I need to remove or clear my activity stack then start a new activity. I don\'t think it is a clear_top flag

4条回答
  •  情书的邮戳
    2020-12-11 02:23

    Use this

    Intent i = new Intent(yourScreen.this,Home.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.putExtra("EXIT", true);
            startActivity(i);
    

    and in the onCreate of the Home class, do this to check,

    if (getIntent().getBooleanExtra("EXIT", false)) 
        {
            Intent i = new Intent(Home.this,Login.class);
            startActivity(i);
            finish();
        }
    

    what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack and take you to the login screen.. Now on the login screen,if you press back button you will exit the app as the stack is cleared..

    Let me know if the problem still persists...

提交回复
热议问题