应用程序打开时调用哪种方法?当应用关闭时?

时间:2016-03-04 00:59:41

标签: java android internal-storage

我需要从文件中检索一个项目的arrayList,但我不知道我需要把这个检索代码放在哪里:

List<ItemCultural> cachedEntries = (List<ItemCultural>) InternalStorage.readObject(this, "arquivo.txt");
        regraDeNegocioSingleton.getListaDeItensSingleton().setListaDeItensCulturais(cachedEntries);

当app通过此命令关闭时,我需要编写此对象:

        InternalStorage.writeObject(this, "arquivo.txt", regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais());

My InternalStorage Class:

我试图在我的MainActivity中覆盖onDestroy但是没有用,我把检索代码放在我的onCreate中,但这种方法总是被调用,而不仅仅是时间。谢谢!

1 个答案:

答案 0 :(得分:0)

您在活动权利中引用了 onCreate onDestroy , 当活动开始时调用 onCreate 方法,当应用关闭时,可以调用 onDestroy 方法。 所以,如果你想在应用程序启动时进行类调用,那么你就是在谈论这个(例子):

public class RemindMe extends Application {

@Override
public void onCreate() {
    super.onCreate();

     //add whatever you want

      }
 }

并将此类添加到您的清单中,如下所示:

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".Model.RemindMe"/>

并在 MainActivity 中添加以下行:

     remindMe = (RemindMe)getApplication();
     remindMe.onCreate();

希望你能解决问题:)