清除活动堆栈并在 android 中启动新活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12403364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear Activity Stack and start new activity in android
提问by shantanu
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 problem. I am explaining with an example:
我的问题与此类问题略有不同。我需要删除或清除我的活动堆栈,然后开始一个新活动。我不认为这是 clear_top 标志问题。我用一个例子来解释:
My Activity flow :
我的活动流程:
Login > Home > Screen1 > screen2 ....
I finish Login activity or call with no_history flag. So my activities are look like this
我完成登录活动或使用 no_history 标志调用。所以我的活动看起来像这样
Login(finished)> Home [bottom of the stack now] > Screen1 > Screen2[top of the stack]
I need to handle session error. If any session error occurs in any point i need to go back to login activity. But remember i don't have login activity in stack. So clear_top will not work.
我需要处理会话错误。如果在任何时候发生任何会话错误,我需要返回登录活动。但请记住,我在堆栈中没有登录活动。所以 clear_top 将不起作用。
If any session error occurs in Screen2 then i need to clear full stack (screen2, screen1, home) and then start login activity. So that after back button press in login activity will close my apps.
如果 Screen2 中发生任何会话错误,那么我需要清除完整堆栈(screen2、screen1、home),然后开始登录活动。这样在登录活动中按下后退按钮将关闭我的应用程序。
Is there any way to clear the activity stack?
有没有办法清除活动堆栈?
thanks in advance
提前致谢
回答by Gautam Mandsorwale
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,
并在 Home 类的 onCreate 中,执行此操作以进行检查,
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..
这本质上的作用是无论您从事什么活动,您都可以使用清晰的顶部标志调用主屏幕。在主屏幕中,onCreate 方法中有一个检查条件,这将有助于清除堆栈并将您带到登录屏幕.. 现在在登录屏幕上,如果您按后退按钮,您将在清除堆栈时退出应用程序..
Let me know if the problem still persists...
让我知道问题是否仍然存在...
回答by AntekM
It's bit old question, but maybe someone else will stumble upon it while searching answer to similar problem.
这是一个老问题,但也许其他人在寻找类似问题的答案时会偶然发现它。
You should start Login activity with flags: Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK
- flag NEW_TASK
may have a little bit confusing name, but it will actually create new task just if it doesn't exist (otherwise current task will be used) - and CLEAR_TASK
will clear it from all previous activities.
您应该使用以下标志启动登录活动:Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK
- 标志的名称NEW_TASK
可能有点令人困惑,但它实际上会在它不存在的情况下创建新任务(否则将使用当前任务)- 并将CLEAR_TASK
从所有以前的活动中清除它。
回答by vinothp
Try this,
尝试这个,
Finish your current Activity
完成您当前的活动
YourCurrentActivity.this.finish();
Intent intent1 = new Intent(YourCurrentActivity.this,LoginActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
It will work even your activity is not in the stack.
即使您的活动不在堆栈中,它也能工作。
Hope it helps.
希望能帮助到你。
回答by ankita gahoi
Use onActivityResult() to manage activities in this scenario.
在这个场景中使用 onActivityResult() 来管理活动。