Search code examples
javaandroidinternal-storage

Which method is called when app open? And when app closes?


I need to retrieve from a file one arrayList of Items, but i dont know where i need to put this retrieving code:

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

And I need to write this object when app closes by this command:

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

My InternalStorage Class:

I tried to Override onDestroy in my MainActivity but dont worked, and i put the retrieving code in my onCreate, but this method always called, not only time. Thanks!


Solution

  • you're referring about onCreate and onDestroy in an activity right, when the activity starts call onCreate method and when the app closes can call onDestroy method. So, if you want a class call when the Application starts you're talking about this (example):

    public class RemindMe extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
    
         //add whatever you want
    
          }
     }
    

    and add this class to your manifest like this:

       <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"/>
    

    and in your MainActivity add this lines:

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

    hope you can solve your issue :)