5

For my current application that I'm writing I have implemented a navigation drawer (the default Android way with backwards compatibility). So from the nav drawer you select a menu element and then I do this (addPreviousToBackStack is always false for testing):

private void replaceFragment(final Fragment fragment, final boolean addPreviousToBackStack) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.activity_main_fragment_container, fragment);
    if(addPreviousToBackStack) {
        fragmentTransaction.addToBackStack(fragment.getTag());
    }
    fragmentTransaction.commit();
    currentFragment = fragment;
}

So that works like a charm when I start the application. Then I close the application using the back button. If I then reopen the app (no matter how: via the long press home button or via the shortcut) the app starts at the initial screen (onCreate is called) and then I open the nav drawer and select a menu item and the application crashes.

This is my exception: "java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState"

And it happens exactly on my line where I do

fragmentTransaction.commit();

I have no clue why I'm getting this when the app is re-opened and not when the app is initially opened. Any clues??

dirkvranckaert
  • 1,364
  • 1
  • 17
  • 30
  • This an Android vintage issue :) Maybe [my answer from a similar question](http://stackoverflow.com/a/13987336/1051783) will help you. – gunar Nov 15 '13 at 10:12
  • Hmm, none of your good ways or Un-orthodox ways work for me... I'm trying the on a 4.3 device (galaxy nexus) – dirkvranckaert Nov 15 '13 at 10:27

1 Answers1

1

I am not sure what is the context of your use case, but calling fragmentTransaction.commitAllowStateLoss(); should not cause the crash anymore. However, you need to assume the risk that your state info will be lost on fragment.

Also, this line currentFragment = fragment; seems to me a cause of memory leak. If Android wants to cleanup the fragment you will prevent it by keeping a strong reference to the fragment. Don't use it ...

gunar
  • 14,660
  • 7
  • 56
  • 87
  • Addding fragmentTransaction.commitAllowStateLoss(); does not work, it keeps on crashing as before... Then how should I keep a reference to the fragment. I sometimes need to interact to the fragment, that's why I keep in there... – dirkvranckaert Nov 15 '13 at 10:23
  • `getFragmentById(R.id.activity_main_fragment_container)` is how you can get the Fragment instance. Then you'll perform an upcast to your fragment class if you need to access a particular method. – gunar Nov 15 '13 at 10:54
  • I tested again with the commitAllowStateLoss() and that seems to give me the Caused by: java.lang.IllegalStateException: Activity has been destroyed exception – dirkvranckaert Nov 15 '13 at 10:56
  • That's easy to fix: check if `getActivity()` is null – gunar Nov 15 '13 at 10:58
  • The activity will never be null as I run this piece of code from the activity... I tried to surround with a check if (!this.isFinishing) {} and that seems to be working, however I'm not sure if I can trust that fix... Can anyone confirm? – dirkvranckaert Nov 15 '13 at 11:05
  • Can you reveal more of your code? I'm interested in the context where you're calling above `replaceFragment(...)` method – gunar Nov 15 '13 at 11:08