Ashis
Ashis

Reputation: 157

Can Application object get re-created while the app still running?

I have extended the Application object as MyApplication and the design is as follows

public class MyApplication extends Application {
  
  private MyObject myObject;
  
  public MyApplication () {
    mInstance = this;
  }
  
  public static MyApplication getInstance() {
    return mInstance;
  }
  
  public MyObject getMyObject(){
    return myObject;
  }
  
  public setMyObject(MyObject object){
    myObject = object;
  }
}

I am accessing the myObject value in Activities as well as Services as MyApplication.getInstance().getMyObject(). I don't see any issue with this normally, but in production for few users sometimes suddenly getMyObject() is returning null i.e getting reset. First I was under the impression that the OS might be killing and re-creating the app due to which I am observing null. I am not avoiding the memory low scenario, however if that is the case it should be killing the Activities and the Services too before destroying the Application object.

Inside an Activity I am keeping a reference of MyApplication as

private MyApplication myApp = MyApplication.getInstance();

So this is how my observation from logs regarding the getMyObject() value.

myApp.getMyObject() prints != null

then a Service MyService gets called and inside that MyApplication.getInstance().getMyObject() prints == null

again back in the activity myApp.getMyObject() or MyApplication.getInstance().getMyObject() prints != null

myObject has been initialized before printing these logs and in between these logs there is no myObject reset code getting called.

Can it happen that the Application object got re-created but the Acitivty is still active? or Can Service get a different instance of Application than that of the Activity thread?

As per my understanding in a app life-cycle myApp should always be equal to MyApplication.getInstance() as OS should maintain single instance of Application.

Note: Also would like to add that I am getting this un-usual behavior in Samsung Tab 4.

Upvotes: 2

Views: 558

Answers (2)

AKroell
AKroell

Reputation: 622

Don't store Data in the Application Object

The applicataion object will not stay in memory forever. When a user sends your application to the background by e.g. pressing the home button the application object might be killed if the system requires the memory. This might happen after minutes but could take hours. When the user then resumes the application the application object might not be the same as before and mMyObject will be null if it has no initial value.

Solutions

Make null checks and act accordingly

Use some form of data-storage

Initialize your object in the application constructor

public MyApplication () {
    mInstance = this;
    mMyObject = new MyObject()
}

Upvotes: 1

NSimon
NSimon

Reputation: 5287

You don't wanna have public constructor, but a onCreate() method. You also want a static instance of the class.

public class MyApplication extends Application {

  private MyObject myObject;
  private static MyApplication mInstance;

  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
  }

  public static MyApplication getInstance() {
    return mInstance;
  }

  public MyObject getMyObject(){
    return myObject;
  }

  public setMyObject(MyObject object){
    myObject = object;
  }
}

And of course change your manifest to tell that's this is your class that will handle the application (if not done yet)

<application
        android:name="com.XXX.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

Upvotes: 0

Related Questions