Android 应用程序退出事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3511741/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:32:37  来源:igfitidea点击:

Android Application On Exit Event

android

提问by Black

I need some help in application exit handling. My application has multiple Activity say : - EntryAct, Act1, Act2 .... Act10.

我需要一些应用程序退出处理方面的帮助。我的应用程序有多个活动说: - EntryAct、Act1、Act2 .... Act10。

When user presses home key on any of these Activities, I need to do some flag setting.

当用户在这些活动中的任何一个上按下主页键时,我需要做一些标志设置。

Handling home key in every application Activity is not possible! Can someone tell how this can be achieved?

在每个应用程序 Activity 中处理 home 键是不可能的!有人可以告诉如何实现这一目标吗?

Can't change anything in OnPause,OnStop or OnDestory of all activities Act1... Act10. :-(

无法更改所有活动 Act1...Act10 的 OnPause、OnStop 或 OnDestory 中的任何内容。:-(

回答by y ramesh rao

your issue can be solved if you use a custom Application class. say like

如果您使用自定义 Application 类,您的问题就可以解决。说喜欢

public class MyApp extends android.app.Application
{
}

and inside this put your code that you want to be called anywhere in your app.

并在其中放置您想要在应用程序中的任何位置调用的代码。

Now only you require to get the application object in your Activitylike this

现在只需要Activity像这样获取应用程序对象

MyApp app = (MyApp) getApplication();
app.setUpBeforeClosingApp();

and you can put this inside every onDestroy()of Activity and from here code handling before closing actions can be achieved.

并且您可以将其放入每个onDestroy()Activity 中,并在完成关闭操作之前从此处进行代码处理。

回答by WarrenFaith

Do it in the onPause()method which is called as soon as the activity is no more in the foreground.

onPause()一旦活动不再在前台,就在调用的方法中执行此操作。

回答by Danny Remington - OMS

First of all it is possible to detect the home key event in an indirect way as shown in my post about killing an application when the home key is pressed: How to close Android application?

首先,可以以间接方式检测主页键事件,如我关于按下主页键时杀死应用程序的帖子所示: 如何关闭 Android 应用程序?

Also it is possible to determine if an activity is the root activity in its onDestroy() method and then have it call a helper class to perform final processing if it is the root activity. Better yet would be to create a custom activity that all of your activities inherit from, place the final processing logic in the onDestroy() method of the custom activity and then have all subclasses call super.onDestroy() in their onDestroy() method. This is similar to what is done in the aforementioned post: How to close Android application?as well as posts about creating setting screens when the menu button is pressed as well as handling the search button and return button.

还可以在其 onDestroy() 方法中确定活动是否是根活动,然后如果它是根活动,则让它调用帮助程序类来执行最终处理。更好的是创建一个所有活动都继承自的自定义活动,将最终处理逻辑放在自定义活动的 onDestroy() 方法中,然后让所有子类在它们的 onDestroy() 方法中调用 super.onDestroy()。这类似于前面提到的帖子中所做的:如何关闭 Android 应用程序?以及有关按下菜单按钮时创建设置屏幕以及处理搜索按钮和返回按钮的帖子。

I hope this helps.

我希望这有帮助。