Hello,
I noticed a strange situation when using Loader in Fragment (in Activity
works well). I init Loader in onActivityCreated() as suggested in
documentation (http://developer.android.com/guide/components/loaders.html)
and after orientation change framework calls twice
method LoaderCallbacks.OnLoadFinished but not in initLoader (probably
beacause Fragment is not in started state, but in docs there is information
that I should be prepared for this
(http://developer.android.com/reference/android/app/LoaderManager.html#initLoader(int,
android.os.Bundle, android.app.LoaderManager.LoaderCallbacks<D>) but it
never happens) . However if I call iniLoader in Fragment.onResume()
everything works as expected. The same issue is described here
https://groups.google.com/forum/?fromgroups#!topic/android-developers/aA2vHYxSskU.
It happens using support library as well. Anyone noticed this situation? I
wrote very simple application to test this issue, here is sample code:
//Activity class
package com.example;
import android.app.Activity;
import android.app.LoaderManager;
import android.os.Bundle;
public class MyActivity extends Activity
{
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LoaderManager.enableDebugLogging(true);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction().add(R.id.container, new
TestFrag(), "asdf").commit();
}
}
}
//Fragment class
package com.example;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.os.Bundle;
import android.util.Log;
public class TestFrag extends Fragment
{
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
LoaderManager.LoaderCallbacks c = new
LoaderManager.LoaderCallbacks()
{
@Override
public Loader onCreateLoader(int id, Bundle args)
{
return new Loader<Object>(getActivity())
{
@Override
protected void onStartLoading()
{
Log.d("TAG", "onStartLoading " + this);
deliverResult(new Object());
}
};
}
@Override
public void onLoadFinished(Loader loader, Object data)
{
//THIS IS CALLED TWICE
Log.d("TAG", "onLoadFinished " + data);
}
@Override
public void onLoaderReset(Loader loader)
{
}
};
getLoaderManager().initLoader(100, null, c);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en