Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to create a new Loader after long periods of inactivity
    primarykey
    data
    text
    <p>I have an Android application whose <code>Fragment</code>s rely on loaders to fetch data. Below is the skeleton code of my <code>Fragment</code>. Everything is the same except I have some custom code in the <code>onLoadFinished</code> method.</p> <pre><code>public class Events extends Fragment implements LoaderCallbacks&lt;ArrayList&lt;Event&gt;&gt; { private Integer intWeek; public static Events newInstance(Integer intWeek) { Events pageFragment = new Events(); pageFragment.intWeek = intWeek; pageFragment.setArguments(new Bundle()); return pageFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.events, null); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLoaderManager().initLoader(this.intWeek, savedInstanceState, this); } public Loader&lt;ArrayList&lt;Event&gt;&gt; onCreateLoader(int intLoader, Bundle bndBundle) { return new Scraper(getActivity().getApplicationContext()); } public void onLoadFinished(Loader&lt;ArrayList&lt;Event&gt;&gt; ldrEvents, final ArrayList&lt;Event&gt; lstEvents) { //Do something with the returned data } public void onLoaderReset(Loader&lt;ArrayList&lt;Event&gt;&gt; ldrEvents) { return; } @Override public void onDestroy() { super.onDestroy(); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } } </code></pre> <p>This <code>Fragment</code> is used inside a <code>FragmentActivity</code> which uses <code>ViewPager</code> which gets its <code>Fragment´s using a</code>FragmentPagerAdapter`.</p> <p>This works fine and I'm able to page between the <code>Fragment</code>s like the one shown above. Every time a new <code>Fragment</code> is added to the <code>ViewPager</code>, the <code>onCreate</code> method fires and it creates a new <code>Loader</code>.</p> <p>When I press the "Home" button on my phone, the application pauses and goes into the background. I can always restore the application and it works just fine — after 5/10/20 minutes of being in the background.</p> <p>...but If I leave the application in the background for a long time, an hour or more, the application crashes upon start and the stacktrace points to the following line in the <code>onCreate</code> method:</p> <pre><code> getLoaderManager().initLoader(this.intWeek, savedInstanceState, this); </code></pre> <p>Now I'm very lost as to why this is happening. It seems that the Android framework destroys something in the background after long periods of inactivity in the background and the <code>initLoader</code> method isn't able to create a <code>Loader</code>. The <a href="http://developer.android.com/reference/android/app/LoaderManager.html#initLoader%28int,%20android.os.Bundle,%20android.app.LoaderManager.LoaderCallbacks%3CD%3E%29" rel="nofollow">documentation about the <code>initLoader</code> method</a> specifically says:</p> <blockquote> <p>Ensures a loader is initialized and active. If the loader doesn't already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used.</p> </blockquote> <p>Would anyone be able to point out what I'm doing wrong here? This seems to be a pretty difficult issue to debug because I can't replicate it at will. It is very random. Thanks</p> <hr> <p>Stacktrace from logcat:</p> <pre><code>Transmitting stack trace: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mridang.stadi/com.mridang.stadi.Main}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) at android.app.ActivityThread.access$600(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4575) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.mridang.stadi.events.Events.onCreate(Events.java:73) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1805) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200) at com.mridang.stadi.Main.onCreate(Main.java:23) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033) ... 11 more </code></pre>
    singulars
    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