Android 退出应用程序
不建议直接退出应用程序,因此重点是完成所有活动,以便所有内容都进入后台并且用户返回主屏幕。我的问题是我有主要活动,它总是使用标志 Intent.FLAG_ACTIVITY_CLEAR_TOP | 启动。 Intent.FLAG_ACTIVITY_SINGLE_TOP 它启动另一个活动,我想在其中放置退出按钮。为了让一切都回到后台,我必须完成当前和主要活动。我虽然使用这些标志和应该退出的额外信息启动主活动可以解决问题,但是随意图传递的额外内容不会到达主活动 - 它仍然获得 android 用于启动应用程序的意图。
换句话说,我尝试过这样的事情:
// Exit's onClick:
Intent intent = new Intent(someContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", true);
context.startActivity(intent);
currentActivity.finish();
// MainActivity onCreate:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
// application never reach this point
boolean exit = extras.getBoolean("exit");
if (exit)
{
finish();
return;
}
}
额外的内容已交付。我怎样才能让它工作?
谢谢
it's not recommended to exit application directly, so the point is to finish all activities so everything goes to the background and user returns to the home screen. My problem is that I have main activity, which is always launched with flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP and it launches another activity, where I want to place the exit button. In order to get everything to the background I have to finish both current and main activity. I though that launching main activity with those flags and extra info that it should exit would do the trick, but the extras delivered with the intent do not reach main activity - it still gets the intent that was used by android to launch the application.
In other words, I tried something like:
// Exit's onClick:
Intent intent = new Intent(someContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", true);
context.startActivity(intent);
currentActivity.finish();
// MainActivity onCreate:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
// application never reach this point
boolean exit = extras.getBoolean("exit");
if (exit)
{
finish();
return;
}
}
The extras are delivered. How can I get it working?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
设置这 2 个标志时,如果 MainActivity 仍在堆栈中,则它不会到达 onCreate()。这将调用 onNewIntent()。
检查这 2 个链接以获取更多信息:
http://developer.android.com /reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
http://developer.android.com/reference /android/app/Activity.html#onNewIntent%28android.content.Intent%29
When setting this 2 flags MainActivity will not reach onCreate() if it is still in the stack. This would call onNewIntent().
Check this 2 links for more information:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29
//您可以使用它来退出整个应用程序,而不仅仅是一个活动
//You can use that to exit the entire application, not just one activity
在
AndroidManifest.xml
文件中将MainActivity
的Launch Mode设置为"SingleTop"
,如下:Set Launch Mode of
MainActivity
as"SingleTop"
inAndroidManifest.xml
file as follows:我不知道这是否是最好的解决方案,但对我来说:
在事件侦听器上:
I don't know if it is the best solution, but work to me:
On Event Listener:
在您的 Activity 上调用 moveTaskToBack(true)
call moveTaskToBack(true) on your Activity
很简单,创建一个类并放置一个静态变量和函数,如下所示:
在您的主活动中放置
YourClassName.setisExit(false);
将确保isExit
值为 false当应用程序再次启动时。退出:
将其放入您的
OnClick
方法中:并将其放入每个活动的
OnResume
方法的第一行:通过在每次活动恢复时执行此操作,检查
isExit
值,如果 true 则退出。您可以在 google play 上查看我的应用
E Player
。我实现了这个方法并且工作正常Simple, make a class and put a static variable and function like this:
And in your Main Activity put
YourClassName.setisExit(false);
is will make sureisExit
value is false when application start again.To Exit:
Put this in your
OnClick
method:And put this in first line of
OnResume
method of every activity:By doing this every time a Activity resume is check the
isExit
value and if true then exit.You can check my app
E Player
on google play. I implemented this method and its working fine