简体   繁体   中英

How to remove activity from backstack in android?

I have an app in which i have three activity namely:- Login,MainActivity and password activity.when I go to password activity and do some event then after login activity comes and here when i press back it will remove login and Mainactivity comes which I don't want. what i want when i press device back twice it will simple close app not to come Mainactivity.How can I archeive this problem.

code that i have tried but not success.

Login code:-

@Override
protected void onResume() {
    super.onResume();
    clearAllTask();
}

private void clearAllTask() {
    CMainActivity m_MainActivity = new CMainActivity();
    if (m_MainActivity.m_MainActivity != null) {
        m_MainActivity.m_MainActivity.finish();
    }

}

and code for MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    m_MainActivity = this;
}

You need to clear the back-stack where you are using the Intent
Like this:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

Here FirstActivity will get cleared from back-stack and will be finished. Also you will be navigated to SecondActivity . So when you press back button from SecondActivity , it will close the app.
Hope this will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM