Putting initLoader in onCreate() causes onLoadFinished() be called twice.

There is also another thread
http://stackoverflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice.
It suggest put initLoader in onCreate() in onResume(). It works but I do
not know why.


2013/8/28 Greenhand <[email protected]>

> Same.
>
> Although it is an old thread, I encounter the same issue, too. I find the
> thread
> http://stackoverflow.com/questions/11001475/listfragment-is-inflated-twice.
> It says put initLoader in onCreate() according to Fragment lifecycle (
> http://developer.android.com/guide/topics/fundamentals/fragments.html#Creating).
> I tried it. It works. Nonetheless, I am not sure if it is a correct way to
> solve the issue.
>
> Ben於 2012年11月23日星期五UTC+8下午10時41分32秒寫道:
>
>> Same
>>
>> On Wednesday, June 27, 2012 4:53:47 PM UTC+3, szakalinhoPL wrote:
>>>
>>> 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<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>)<http://developer.android.com/reference/android/app/LoaderManager.html#initLoader(int,+android.os.Bundle,+android.app.LoaderManager.LoaderCallbacks%3CD%3E)>
>>> 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<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
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Android Developers" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/android-developers/vc4-RUri9k8/unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to