Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android compatibility package - fragment ... not attached to Activity

Tags:


Hello, I'm facing this problem : I'm using Compatibility package to use fragment in android application (min SDK 2.1).

An random exception occurs on fragment sometimes and I can't figure out why. This is the stack trace I receive:

java.lang.IllegalStateException: Fragment FeedListFragment{438a54e8} not attached to Activity     at android.support.v4.app.Fragment.getLoaderManager(Fragment.java:715)     at com.myappli.ui.FeedListFragment.refreshUpdateDate(FeedListFragment.java:283)     at com.myappli.ui.phone.FeedListActivity.onReceiveResult(FeedListActivity.java:277)     at com.myappli.data.rssplayer.service.KTDetachableResultReceiver.onReceiveResult(KTDetachableResultReceiver.java:55)     at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)     at android.os.Handler.handleCallback(Handler.java:587)     at android.os.Handler.dispatchMessage(Handler.java:92)     at android.os.Looper.loop(Looper.java:136)     at android.app.ActivityThread.main(ActivityThread.java:4425)     at java.lang.reflect.Method.invokeNative(Native Method)     at java.lang.reflect.Method.invoke(Method.java:521)     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)     at dalvik.system.NativeStart.main(Native Method) 

Here is the corresponding code I'm calling in the Fragment class:

public class FeedListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>, FeedListCursorAdapterListener {  ...     public void refreshUpdateDate() {     getLoaderManager().restartLoader(LAST_UPDATE_CURSOR_ID, null, this);     } ... } 

Here is the code of the activity that calls the fragment:

private FeedListFragment mCursorLoaderListFg;  if (!isFinishing()) {     mCursorLoaderListFg.refreshUpdateDate();     mCursorLoaderListFg.refreshDisplay();     mCursorLoaderListFg.hideLoadingArticles(); } 

Here is the fragment source code for getLoaderManager():

/** * Return the LoaderManager for this fragment, creating it if needed. */ public LoaderManager getLoaderManager() {     if (mLoaderManager != null) {         return mLoaderManager;     }     if (mActivity == null) {         throw new IllegalStateException("Fragment " + this + " not attached to Activity");     }     mCheckedForLoaderManager = true;     mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true);     return mLoaderManager; } 

Here are the steps of the application before crash:

  1. The activity calls methods in background.
  2. Callback methods are called on the activity
  3. In this callback method, the activity calls the fragment
  4. The fragment tries to launch some methods (like getLoadManager()) that need activity.
  5. The fragment is not attached anymore to the activity => crash

We tried two fixes:

  • test on activity to check if the activity is finishing (like the code above)
  • test on the fragment with the method isDetached.

Thanks for your help!!!

Do not hesitate to ask me more information if I'm not clear.

like image 871
Seynorth Avatar asked Jul 29 '11 08:07

Seynorth


People also ask

How do I attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Can we use fragment without activity in Android?

It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity.

Is called when fragment is no longer connected to the activity?

onStop() — This method called when the fragment is no longer visible; the fragment will get change with other fragment or it gets removed from activity or fragment's activity called stop.


2 Answers

I had a similar problem.

Try using isAdded() or isVisible() instead of isDetached(). Unfortunately, isDetached() doesn't seem to work like it should...

like image 141
Pikaling Avatar answered Sep 19 '22 11:09

Pikaling


What that worked for me was to use the Fragment listener of the fragment, add a getActivity function for it that returns the activity of the Fragment listener (the activity that created the fragment).

Now when i have a call that needed the activity i just use the function.

like image 43
oznus Avatar answered Sep 20 '22 11:09

oznus