java.lang.IllegalStateException: No activity

删除回忆录丶 提交于 2019-11-28 09:20:45
2Dee

Finally found out what this was about: the "no activity" crash was due to the fact that I was using a wrong FragmentManager to nest Fragments.

For nested fragments, the only valid FragmentManager is the one obtained in the containing Fragment, by calling getChildFragmentManager().

I got this error too. Finally, found out that I was overriding the methods onResume() onStop() incorrectly

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
@Override
protected void onStop() {
    super.onResume();
    finish();
}

Changed it to

 @Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
@Override
protected void onStop() {
    super.onStop();
    finish();
}

So Silly Mistake :P

You need that the transaction to be committed after an activity's state is saved...

Use ft.commitAllowingStateLoss() instead of ft.commit()

That will solve your problem.

Source

Chris Sprague

I was creating a fragment in myActivity.onCreate() by calling:

getSupportFragmentManager()
    .beginTransaction()
    .remove(frag)
    .replace(idOfViewBeingReplaced, frag)
    .commit();

While viewing my app I would hit the power-off button and then the app would crash. Apparently, when the device is powered off, the Activity and Fragment states are saved, detached, and then onCreate() and onCreateView() are called on the Fragments. In this state, .remove(frag) cannot be called or else it will throw:

illegal state: no activity

So, I just deleted .remove(frag) and everything is working again.

In my case I using a splash screen and I have had MainLauncher = true in the MainActivity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!