As specified in the Android documentation, it is not guaranteed that will be called when exiting your application.onDestroy()
"There are situations where the system will simply kill the activity's hosting process without calling this method"
https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
Instead, you can create a service which will be notified when the Task your activities are running inside is destroyed.
Create the service class:
public class ClosingService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
// Handle application closing
fireClosingNotification();
// Destroy the service
stopSelf();
}
}
Declare / register your service in the manifest (within the application tag, but outside any activity tags):
<service android:name=".services.ClosingService"
android:stopWithTask="false"/>
Specifying will cause the method to be triggered in your service when the task is removed from the Process.stopWithTask="false"
onTaskRemoved()
Here you can run your closing application logic, before calling to destroy the Service.stopSelf()
You should read some info about Activity lifecycle. There is one thing about onDestroy method, it doesn't get called all time. You mustn't rely on it.
Specify please what are you trying to achive and I'll try to offer better solution.
Suggestion
So, if I understood you right, I can suggest one thing. Start a that will fire every N seconds (it's not really heavy to system). Register and for this broadcast in . This way you'll get or depending on if there is any that can catch your . And if no receivers than check for some value that indicates if was pressed.Service
LocalBroadcast
BroadcastReceiver
Activities
true
false
BroadcastReceiver
LocalBroadcast
SharedPreferences
Button
-
如何在整个字符串中搜索特定单词? 我有这个代码,它搜索一个字符串数组,如果输入字符串与字符串的第一个字符匹配,则返回结果: 但是我想得到那些字符串,其中输入字符串不仅在第一个字符中匹配,而且在字符串中的任
-
添加Android应用程序的快捷方式到主屏幕打开按钮点击 我想通过按下按钮轻松将我的应用程序添加到主屏幕。因此,我在想的是我的应用程序底部的一个按钮,上面写着“添加到主屏幕”,当按下它时,它会在不关闭应用程序的情况下将快捷方式添
-
将屏幕亮度控制添加到安卓应用程序 我希望在我的应用程序菜单中添加控件以在本地调整屏幕亮度,但似乎不知道如何操作。我已经看到了最大化或调暗亮度的示例,但我希望添加控件,以便用户可以控制和设置亮度级别。有没有
-
-
由以下原因引起:java.lang.IllegalStateException: ParsePlugins 已被初始化 我退出了应用程序,重新启动了它,我得到了一个例外。 编辑: 我想知道为什么Parse会为此抛出一个异常。为什么不继续前进。它已初始化,如果我再次初始化它,那就太大了。 溶液 我已