Very simple code, but got error "Activity has been destroyed" when use Fragment
Solution 1
I also faced a similar problem.
I realized that this happened because the activity was being destroyed while the FragmentTransaction
was about to get .commit()
.
A solution to this was to check whether the Activity.isFinishing()
is true or not.
if (!isFinishing()) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(SOME_RES_ID, myFragmentInstance);
ft.commit();
}
Solution 2
I figured out myself, It is because I missed the super.onCreate(savedInstanceState);
in my Activity onCreate() method . After added this, things are fine.
Solution 3
I faced the same issue and unable to fix it. Event I added isFinishing() check as well. but no luck.
then I added one more check isDestroyed() and its working fine now.
if (!isFinishing() && !isDestroyed()) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(LAYOUT_ID, myFragmentInstance);
ft.commit();
}
Solution 4
To give an explanation:
The framework creates the Activity
and calls Activity.onCreate()
.
Activity.onCreate()
will somehow attach to the FragmentManager
to let it know about the hosting activity. Activity.onDestroy()
will unregister it again.
Now, you extend Activity
and override onCreate()
. You make calls to FragmentManager
without calling through toActivity.onCreate()
. The whole registration logic explained above is not executed. The FragmentManager
therefore does not know anything about the activity and assumes it has already been destroyed and generates the exception with the misleading error message.
Solution 5
Just to make sure I'm seeing this right, MyActivity is the activity that you're trying to launch and then add an instance of FirstFragment into, right?
Immediately I see two things that you need to look at
1) In your activity you never call setContentView() or super.onCreate(). The first call could be a huge issue for you because that means that your layout was never inflated, and therefore, the R variable doesn't exist
2) Your MyActivity needs to have its own xml layout file, which it doesn't have.
Related videos on Youtube
Comments
-
Leem.fin about 4 years
I made a very simple Activity which shows a simple ListFragment like below:
My Activity:
public class MyActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { FragmentManager fragMgr = getSupportFragmentManager(); FirstFragment list = new FirstFragment(); fragMgr.beginTransaction().add(android.R.id.content, list).commit(); } }
My ListFragment:
public class FirstFragment extends ListFragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.main, null); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String listContent[] = {"Larry", "Moe", "Curly"}; setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, listContent)); } ... }
When I start my app, I got error message:
java.lang.IllegalStateException: Activity has been destroyed E/AndroidRuntime( 947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) E/AndroidRuntime( 947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) E/AndroidRuntime( 947): at android.app.ActivityThread.access$2200(ActivityThread.java:119) E/AndroidRuntime( 947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) E/AndroidRuntime( 947): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 947): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 947): at android.app.ActivityThread.main(ActivityThread.java:4363) E/AndroidRuntime( 947): at java.lang.reflect.Method.invokeNative(Native Method) ...
It complains that Activity has been destroyed, Why???
P.S. main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="next" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false" /> <TextView android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF0000" android:text="No data" /> </LinearLayout>
-
Leem.fin about 12 yearsHi, I don't think an Activity must have a layout file, for example, my code is working after I added super.onCreate(...), my Activity just hosts a Fragment, and the Fragment will inflate a layout to show, not the Activity.
-
Leem.fin about 12 yearsIf some one can explain the deep reason, I will accept the better one not mine :)
-
David C. Sainte-Claire about 12 yearsIt's possible since I haven't really used the support package much. Most of my development is on Honeycomb using multi-pane layouts. I was just looking at the API docs and I think you're right. Thanks for pointing this out!
-
Leem.fin about 12 yearsNo problem, I am also new to the support package, I start to learn the fragment just from today :)
-
Ankit about 10 yearsIn my case I was calling super.onCreate(savedInstanceState); after adding the fragment.
-
Paresh Mayani about 8 yearsThanks it worked for me! Came here through a discussion over Github. Thanks for sharing.
-
Gira about 7 yearsNote that
onDestroyed()
was added in API level 17. -
Saurabh over 5 years@lemm.fin I was going through this article and found its explained quite well over there. medium.com/@elye.project/…