I start my application first time, everything works clean. I finish my activity using finish().
After this, I start my application again and it crashes with this log:
02-19 15:33:16.652: E/AndroidRuntime(1859): FATAL EXCEPTION: main
02-19 15:33:16.652: E/AndroidRuntime(1859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fileexplorermanager/com.landa.fileexplorermanager.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.os.Looper.loop(Looper.java:137)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 15:33:16.652: E/AndroidRuntime(1859): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 15:33:16.652: E/AndroidRuntime(1859): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 15:33:16.652: E/AndroidRuntime(1859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 15:33:16.652: E/AndroidRuntime(1859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 15:33:16.652: E/AndroidRuntime(1859): at dalvik.system.NativeStart.main(Native Method)
02-19 15:33:16.652: E/AndroidRuntime(1859): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
02-19 15:33:16.652: E/AndroidRuntime(1859): at com.landa.features.BrowseHandler.populateContent(BrowseHandler.java:137)
02-19 15:33:16.652: E/AndroidRuntime(1859): at com.landa.features.BrowseHandler.openFile(BrowseHandler.java:98)
02-19 15:33:16.652: E/AndroidRuntime(1859): at com.landa.fileexplorermanager.MainActivity.onCreate(MainActivity.java:33)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.Activity.performCreate(Activity.java:5104)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 15:33:16.652: E/AndroidRuntime(1859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 15:33:16.652: E/AndroidRuntime(1859): ... 11 more
It appears the problem is populateContent() function which calls FragmentManager’s commit – that’s the line it’s failing on:
public void populateContent(File f)
{
FragmentTransaction transaction = ac.getSupportFragmentManager().beginTransaction();
ContentFragment cf = new ContentFragment(ctx, ac, this, f);
transaction.replace(R.id.contentFragment, cf);
transaction.addToBackStack(null);
updateShownPath(f.getPath());
transaction.commit();
}
Now, I have no idea why would an application work fine the first time, then finish(), then crash on 2nd start.
Any hints?
Note: I do have one fragment in FragmentManager before finishing the app (ALWAYS) – that’s the home fragment.
This is the onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialiseApplication();
browseHandler.openFile(new File(BrowseHandler.current_path));
}
It seems the reason for this error that you are calling
commitin a context that already got destroyed i.e. Activity that finished.Make sure that your
ctxvariable refers to an activeActivity. Alternatively, you might want to keep yourActivityin the background instead of manually finishing it.