在应用程序终止或终止期间调用 android 应用程序中的方法

3 android android-intent

我是安卓开发的新手。我正在开发一个 android 应用程序。

当用户杀死或终止我的应用程序时,谁能告诉我如何在 android 中调用特定方法。换句话说,当用户从他们的手机杀死/终止我的应用程序时,我需要首先执行一个特定的方法。因此,将首先执行特定方法,然后关闭我的应用程序。

请提出任何建议。谢谢你。

Leo*_*die 5

这通过重写是可能的onStoponDestroy你的方法Activity

像这样:

public class MyActivity extends Activity{

     //Other methods here (onCreate, and such)

     @Override
     public void onStop()
     {
         super.onStop();
         //Do whatever you want to do when the application stops.
     }
     @Override
     public void onDestroy()
     {
         super.onDestroy();
         //Do whatever you want to do when the application is destroyed.
     }

}
Run Code Online (Sandbox Code Playgroud)
  • onStop 在用户退出活动之前或当另一个活动开始在后台推送此活动时被调用。
  • onDestroy 在您通过调用 Activity.Finish() 完全完成活动之前,或者在系统必须将其从内存中删除以节省空间时调用。

有关应用程序生命周期的更多信息,请参见此处