I\'m building an Android app for which I\'d like my first activity to be composed of 2 tabs, one for the user\'s profile, and one for the friends\' activity. For these tabs,
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
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 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.
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