Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my loader destroyed?
    primarykey
    data
    text
    <p>I have a Fragment which uses a Loader. The Fragment implements LoaderManager.LoaderCallbacks. In the <code>onCreate()</code> of the fragment I do a <code>setRetainInstance(true)</code> so that the Fragment is not destroyed across configuration changes like orientation change. This works as expected and the <code>onDestroy()</code> for the fragment is not called. However, my loader is destroyed when I rotate the device. Each time I rotate the device a new Loader is created. I checked the memory dump and verified this.</p> <ol> <li>Why is this happening</li> <li>How can I avoid this ? </li> </ol> <p><strong>EDIT</strong>: To avoid confusion : The local variable loader is not relevant here. It is just to check if my previously created loader exists or not. Even with the updated code below (removed the local variable), the loader is destroyed. </p> <p>Here is some relevant code:</p> <pre><code> public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks&lt;Object&gt; { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); //Don't destroy fragment across orientation change } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(TAG, "onActivityCreated, myTid : " + android.os.Process.myTid()); mLoaderManager = getLoaderManager(); mLoaderManager.initLoader(MY_LOADER, null, this); } @Override public Loader&lt;Object&gt; onCreateLoader(int i, Bundle bundle) { Log.d(TAG, "onCreateLoader, myTid : " + android.os.Process.myTid()); return new MyLoader(getActivity().getApplicationContext()); } @Override public void onLoaderReset(Loader&lt;Object&gt; objectLoader) { Log.d(TAG, "onLoaderReset, myTid : " + android.os.Process.myTid()); mHandler.post(new Runnable() { @Override public void run() { if (mTxtSomeTxt != null) { mTxtSomeTxt.setText(""); } } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload