简体   繁体   English

如何从Android中的Backstack中删除活动?

[英]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. 我有一个应用程序,其中有三个活动,即:-登录,MainActivity和密码活动。当我进入密码活动并执行一些活动,然后在登录活动出现后,在这里按回去它将删除登录,并且我会退出Mainactivity不想要 what i want when i press device back twice it will simple close app not to come Mainactivity.How can I archeive this problem. 当我按两次设备返回时,我想要的是简单的关闭应用程序而不会出现Mainactivity。我如何归档此问题。

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 和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 您需要清除使用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. 在这里, FirstActivity将被从后栈清除并完成。 Also you will be navigated to SecondActivity . 同样,您将导航到SecondActivity So when you press back button from SecondActivity , it will close the app. 因此,当您从SecondActivity按下返回按钮时,它将关闭该应用程序。
Hope this will solve your problem. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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