I have 1 activity and 2 fragments. Both fragments use the custom AsyncTaskLoader to retrieve some data from the web service, and since I use Loader, it needs to store activity data and re-create fragments. Both fragments override the onActivityCreated method and call getLoaderManager (). InitLoader (0, null, this), which either creates a new one or reuses the existing bootloader.
When an activity is first created, it adds Fragment # 1 to FrameLayout by default, loads the data, internally calls the LoaderCallbacks.onLoadFinished () method and displays the result. I have a button that replaces fragment # 1 with fragment # 2 per click, and fragment # 1 with a fragment-stack fragment. When the user presses the BACK key, he switches to Snippet # 1.
onActivityCreated is called again on fragment # 1, and then again calls iniLoader (). This time, the data already exists in the loader, and I expect it to call the LoaderCallbacks.onLoadFinished method again, since it already has data available, as described here: http://goo.gl/EGFJk
Provides initialization and activation of the bootloader. If the bootloader does not exist yet, it is created and (if the activity / fragment is currently running) starts the bootloader. Otherwise, the last bootloader created will be reused. In any case, this callback is associated with the loader and will be called as a change in the state of the loader. If the calling subscriber is in a running state and the requested bootloader already exists and generated its data, then callback onLoadFinished (Loader, D) will be called immediately (inside this function), so you should be prepared for this to happen.
But this method is never called, even if the loader exists and generated data ready to be sent.
Change # 1 Problem from the point of view of users:
- The user starts the activity and sees fragment1 with some data
- The user clicks on what changes the first fragment to another, with different data
- User presses the BACK key
- The user now looks at fragment1 again, but there is no data. (which means I need to get it from the webservice again - and I would like to avoid this if possible).
Here is my activity:
public class FragmentTestsActivity extends Activity implements OnClickListener { private Button btn1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(this); Fragment newFragment = new Fragment1(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(R.id.fragmentContainer, newFragment).commit(); } @Override public void onClick(View view) { int id = view.getId(); if (id == R.id.btn1) { showNewFragment(); } } public void showNewFragment() {
My snippet # 1:
public class Fragment1 extends Fragment implements LoaderCallbacks<String> { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); LoaderManager.enableDebugLogging(true); getLoaderManager().initLoader(0, null, this); } private static class TestLoader extends AsyncTaskLoader<String> { String result; public TestLoader(Context context) { super(context); } @Override public String loadInBackground() {
Does anyone know a solution to this or what I'm doing wrong here? Any help is appreciated.