I was using Eclipse to compile the app, I used to get this error but not a lot. But since I've switch to android studio, I'm getting this crash a lot. Its crashing pretty much every device. I've tried doing everything but can't seem to fix this issue. I've tried using both getfragmentManager() and getSupportFragmentManager(), getting this crash in both. Not sure what to do now?
Below is the code I'm using to switch fragments.
private void showFragment(Fragment fragment){
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
if (!fragment.isAdded()) {
fragmentTransaction.replace(R.id.home_frame, fragment).commitAllowingStateLoss();
currentFragment = fragment;
}
}
And all my fragments extends a base fragment which has the following code
private static final Field sChildFragmentManagerField;
static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.wtf("LOGTAG", "Error getting mChildFragmentManager field", e);
}
sChildFragmentManagerField = f;
}
@Override
public void onDetach() {
super.onDetach();
if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
Log.e("LOGTAG", "Error setting mChildFragmentManager field", e);
}
}
}
Please help out. I'm getting tonnes of crashes and bad reviews on the store.
Below is my stacktrace
Fatal Exception: java.lang.IllegalStateException: No activity
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1054)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1871)
at android.app.Fragment.performActivityCreated(Fragment.java:2067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
Okay I searched everywhere I couldn't find the fix. But making the following two changes I was able to fix the problem.
First, instead of passing the getChildFragmentManager() every time as an argument, I created an object in the application class and used that.
Second, Apparently I was using getChildFragmentManager() twice in the same fragment ie I had two nested fragments. I removed one.