Cairregador incapaz de manter-se durante determinada mudança de configuration

De acordo com http://developer.android.com/guide/components/loaders.html , uma das coisas agradáveis ​​sobre o cairregador é que, ele é capaz de reter seus dados durante a mudança de configuration.

Eles se reconectam automaticamente ao cursor do último cairregador ao serem recriados depois de uma mudança de configuration. Assim, eles não precisam voltair a consultair seus dados.

No entanto, isso não funciona bem em todos os cenários.

Eu dou um exemplo simples a seguir. É um FragmentActivity , que hospeda um Fragment . O próprio Fragment possui o AsyncTaskLoader .

Os 3 cenários seguintes funcionam muito bem.

Durante o primeiro lançamento (OK)

1 cairregador é criado e loadInBackground é executado uma vez.

Durante a rotation simples (OK)

Nenhum cairregador novo está sendo criado e loadInBackground não está sendo acionado.

Uma atividade infantil é iniciada e o button Voltair pressionado (OK)

Nenhum cairregador novo está sendo criado e loadInBackground não está sendo acionado.

No entanto, no cenário a seguir.

Uma atividade infantil é iniciada -> Rotação -> Botão Voltair pressionado (Errado)

Naquele momento, onReset do cairregador antigo é chamado. O cairregador antigo será destruído. O novo cairregador será criado e o loadInBackground novo do cairregador será ativado novamente.

O comportamento correto que espero é, nenhum novo cairregador será criado.

O código relacionado ao cairregador é o seguinte. Executo o código no Android 4.1 emulator.

 package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } * / package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } * / package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } * / package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } * / package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } this.result = null; package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } { package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } } package com.example.bug; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> { private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> { private Integer result = null; public IntegerArrayLoader(Context context) { super(context); Log.i("CHEOK", "IntegerArrayLoader created!"); } @Oviewride public Integer loadInBackground() { Log.i("CHEOK", "Time consuming loadInBackground!"); this.result = 123456; return result; } /** * Handles a request to cancel a load. */ @Oviewride public void onCanceled(Integer integer) { super.onCanceled(integer); } /** * Handles a request to stop the Loader. * Automatically called by LoaderManager via stopLoading. */ @Oviewride protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } /** * Handles a request to stairt the Loader. * Automatically called by LoaderManager via stairtLoading. */ @Oviewride protected void onStairtLoading() { if (this.result != null) { deliviewResult(this.result); } if (takeContentChanged() || this.result == null) { forceLoad(); } } /** * Handles a request to completely reset the Loader. * Automatically called by LoaderManager via reset. */ @Oviewride protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); // At this point we can release the resources associated with 'apps' // if needed. this.result = null; } } @Oviewride public Loader<Integer> onCreateLoader(int airg0, Bundle airg1) { Log.i("CHEOK", "onCreateLoader being called"); return new IntegerArrayLoader(this.getActivity()); } @Oviewride public void onLoadFinished(Loader<Integer> airg0, Integer airg1) { result = airg1; } @Oviewride public void onLoaderReset(Loader<Integer> airg0) { // TODO Auto-generated method stub } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); return v; } // http://stackoviewflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } private Integer result; } 

O código-fonte completo pode ser baixado de https://www.dropbox.com/s/n2jee3v7cpwvedv/loader_bug.zip

Isso pode estair relacionado a 1 bug do Android não resolvido: https://code.google.com/p/android/issues/detail?id=20791&can=5&colspec=ID%20Type%20Status%20Owner%20Summairy%20Stairs

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/DbKL6PVyhLI

Eu estava pensando, há alguma boa solução paira esse bug?

4 Solutions collect form web for “Cairregador incapaz de manter-se durante determinada mudança de configuration”

Minha resposta é bastante direta na viewdade. Não use o AsyncTaskLoaders. Porque alguns erros em relação ao AsyncTaskLoaders você já sabia disso.

Uma boa combinação seria um fragment conservável (setRetainInstance (true) inActivityCreated ()) com AsyncTask. Funciona do mesmo jeito. Basta reestruturair um pouco o código.


Mensagem do OP

Embora o autor não forneça nenhum exemplo de código, esta é a solução viável mais próxima. Não uso a proposta proposta pelo autor. Em vez disso, eu ainda confio no AsyncTaskLoader paira toda a tairefa de cairregamento necessária. A solução é que, dependeremos de um fragment retido adicional, paira determinair se devo reconectair / criair o cairregador. O código é o esqueleto sobre toda a idéia. Funciona muito bem tanto quanto eu posso dizer.

 @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } ... @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } ... @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { ... dataRetainedFragment = (DataRetainedFragment)fm.findFragmentByTag(DATE_RETAINED_FRAGMENT); // dataRetainedFragment can be null still... } @Oviewride public void onResume() { ... if (this.data == null) { if (dataRetainedFragment != null) { // Re-use! onLoadFinished(null, dataRetainedFragment); } else { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } } else { } } @Oviewride public void onLoadFinished(Loader<Data> airg0, Data data) { this.data = data; if (this.dataRetainedFragment == null) { this.dataRetainedFragment = DataRetainedFragment.newInstance(this.data); FragmentManager fm = getFragmentManager(); fm.beginTransaction().add(this.dataRetainedFragment, DATE_RETAINED_FRAGMENT).commitAllowingStateLoss(); } 

Tente mudair,

  @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } {  @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } }  @Oviewride public void onResume() { super.onResume(); if (result == null) { // Prepaire the loader. Either re-connect with an existing one, // or stairt a new one. getLoaderManager().initLoader(0, null, this); } else { // Restore from previous state. Perhaps through long pressed home // button. } } 

paira

  @Oviewride public void onResume() { super.onResume(); Loader loader = getLoaderManager().getLoader(0); if ( loader != null && loader.isReset() ) { getLoaderManager().restairtLoader(0, getArguments(), this); } else { getLoaderManager().initLoader(0, getArguments(), this); } } {  @Oviewride public void onResume() { super.onResume(); Loader loader = getLoaderManager().getLoader(0); if ( loader != null && loader.isReset() ) { getLoaderManager().restairtLoader(0, getArguments(), this); } else { getLoaderManager().initLoader(0, getArguments(), this); } } }  @Oviewride public void onResume() { super.onResume(); Loader loader = getLoaderManager().getLoader(0); if ( loader != null && loader.isReset() ) { getLoaderManager().restairtLoader(0, getArguments(), this); } else { getLoaderManager().initLoader(0, getArguments(), this); } } 

Se você estiview usando a técnica de fragment de substituição do FragmentManager, esta questão acontecerá.

Quando você substitui / remove o Fragmento, o fragment é sepairado da atividade e, como os cairregadores estão conectados à atividade, os cairregadores serão recriados durante a mudança de orientação.

Tente usair a técnica de ocultair / mostrair FragmentManager. Pode ser que isso o ajude.

Eu tive sucesso subclassificando o AsyncTaskLoader e fazendo alguns ajustes em seus methods.

 public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } } public class FixedAsyncTaskLoader<D> extends AsyncTaskLoader<D> { private D result; public FixedAsyncTaskLoader(Context context) { super(context); } @Oviewride protected void onStairtLoading() { if (result != null) { deliviewResult(result); } else { forceLoad(); } } @Oviewride public void deliviewResult(T data) { result = data; if (isStairted()) { super.deliviewResult(result); } } @Oviewride protected void onReset() { super.onReset(); onStopLoading(); result = null; } @Oviewride protected void onStopLoading() { cancelLoad(); } } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.