1

I was wondering, during activity re-creation, or fragment re-creation or service re-creation, is there a possibility, that the same instance of class being re-used?

For example

//public class HomeFragment extends Activity {
//public class HomeFragment extends Service {
public class HomeFragment extends Fragment {

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

        // Can instrumentSearchMonitor has possibility to become null right here, due to onDestroy?
        instrumentSearchMonitor.doSomething();
    }

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

        instrumentSearchMonitor = null;
    }

    private InstrumentSearchMonitor instrumentSearchMonitor = new InstrumentSearchMonitor();
}

For the above case, is there a possibility that onCreate will encounter a null object, due to nullify action in onDestroy?

My testing is, after onDestroy is being called, the next call of onCreate will happen on the different class instance.

I was wondering, is there any possibility, that the next call of onCreate, will happen on the same class instance?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

2 Answers2

1

Once onDestroy() has been called, the fragment is going to be completely removed and can not be reused.

This can be seen in the Fragment lifecycle:
https://i.stack.imgur.com/fRxIQ.png

In the image you can seee that onDestroyView() can be called, and then it goes back to onCreateView(), but once onDestroy() is called, you're safe to null/delete whatever you want in that class instance.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
0

Put your code above onDestroy

@Override
    public void onDestroy() {
        instrumentSearchMonitor = null;
        super.onDestroy();

    }
MrTy
  • 140
  • 1
  • 9