0

I want to get clarity on loading of classes, destruction of objects etc in android because I noticed some weird things happening when using Singleton in My Activity. Best I will describe it using code :
My Singleton class

public class FilterCriteria  {

    private final String TAG=FilterCriteria.class.getSimpleName();
    private static FilterCriteria filterCriteria=new FilterCriteria();

    private FilterCriteria()
    {

    }

    public static FilterCriteria getInstance()
    {
        return filterCriteria;
    }

    private int rentUpperBound,rentLowerBound;
    private int bedrooms,baths;
    private float distance;
    private ObjectStateListener listener;

    public void setFilters(float distance,int baths,int bedrooms,int rentLowerBound,int rentUpperBound) {

        this.distance = distance;
        this.baths=baths;
        this.bedrooms=bedrooms;
        this.rentLowerBound=rentLowerBound;
        this.rentUpperBound=rentUpperBound;
        if(listener!=null)
            listener.onObjectStateChanged();
    }

    public void attachListener(ObjectStateListener listener) {
        if (this.listener == null) {
            this.listener = listener;
            Log.v(TAG, "NO LISTENER PRESENT AS EXPECTED");
        } else {
            Log.v(TAG, "LISTENER PRESENT!!! BUT THE ACTIVITY WAS STARTED JUST NOW.");
        }
    }

     public void destroy()
    {
        filterCriteria=null;
    }

}

The attachListener(ObjectStateListener listener) function is called only once in the activity. So, when I open my activity the first time, I get this log from attachListener function

NO LISTENER PRESENT AS EXPECTED

Now, I close the activity and then reopen it. But now I get this log

"LISTENER PRESENT!!! BUT THE ACTIVITY WAS STARTED JUST NOW."

So, that means the object still lives on even after the activity (and the application) was closed. Is this normal???
So, I tried to destroy the singleton instance using the destroy() function in the onDestroy() function of Activity.

@Override
      protected void onDestroy(){
            filterCriteria.destroy();//Trying to destroy the singleton
            super.onDestroy();
            Log.v(TAG,"Destroying activity");
           }


But I got NullPointerException on this line filterCriteria.destroy(). So, that means android has already made object null, whereas when I see in debug mode, other members of the Activity are still alive. Why is only this null?

What is happening!???

Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • Well... I think you know where the static instances live... Directly under the application scope, which is outside the Activity scope. – sarveshseri Jan 30 '15 at 09:06
  • Better discussed in this STO question.. http://stackoverflow.com/questions/1944369/android-static-object-lifecycle-application-act-crazy – sarveshseri Jan 30 '15 at 09:08
  • @SarveshKumarSingh then why is only the filterActivity null when other member of the Activity ( static and non-static) live on. – Ashwin Jan 30 '15 at 09:09
  • Is FilterCriteria.getInstance().destroy() equals to your filterCriteria.destroy()? For 'NullPointerException', better attach logcat of exception – Xcihnegn Jan 30 '15 at 09:23

1 Answers1

0

When you invoke the method attachListener() you are creating a reference to the linked object (even if it is static): this reference will be binded to the activity lifecycle.

On the other hand, filterCriteria will follow the static field Java-like lifecycle (but you can still remove this reference manually).

bonnyz
  • 13,458
  • 5
  • 46
  • 70