Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting exception as Failure saving state: active Fragment has cleared index: -1 when I am pressing home button of android device

I am facing some weird issue while I am pressing home button of android and while starting new activity.

My stacktrace is as follows

05-13 18:06:21.182: E/FragmentManager(5674): Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:21.182: E/FragmentManager(5674): Activity state:
05-13 18:06:22.032: E/ACRA(5674): ACRA caught a IllegalStateException exception for com.itgurussoftware.android.dineback. Building report.
05-13 18:06:27.732: E/ACRA(5674): com.itgurussoftware.android.dineback fatal error : Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:27.732: E/ACRA(5674): java.lang.IllegalStateException: Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:27.732: E/ACRA(5674):   at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1723)
05-13 18:06:27.732: E/ACRA(5674):   at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.Activity.performSaveInstanceState(Activity.java:1113)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2793)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2851)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.access$900(ActivityThread.java:123)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1164)
05-13 18:06:27.732: E/ACRA(5674):   at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:06:27.732: E/ACRA(5674):   at android.os.Looper.loop(Looper.java:137)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.main(ActivityThread.java:4424)
05-13 18:06:27.732: E/ACRA(5674):   at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:06:27.732: E/ACRA(5674):   at java.lang.reflect.Method.invoke(Method.java:511)
05-13 18:06:27.732: E/ACRA(5674):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-13 18:06:27.732: E/ACRA(5674):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-13 18:06:27.732: E/ACRA(5674):   at dalvik.system.NativeStart.main(Native Method)

and my code to add and remove the fragment is as follows

        FragmentContainer fragContainer = container.get(container.size() - 1);
        Fragment fragment = fragContainer.getFragment();
        String tag = fragContainer.getTabName();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();

        mTabHost.setCurrentTabByTag(tag);

        if (!fragment.isAdded()) {

            ft.remove(fragment);
        }
        if(container.size()>0)
            container.remove(container.size() - 1);
        ft.replace(R.id.realtabcontent, fragment);
        ft.commitAllowingStateLoss();

Tried

  • Updating support library
  • Tried overriding onSaveInstanceState as

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        if (outState != null) {
            super.onSaveInstanceState(outState);
        }
    }
    
like image 588
NullPointerException Avatar asked Oct 31 '22 23:10

NullPointerException


1 Answers

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, this, TAG_TAFFER_TIME);
    mFragmentHolder = (RelativeLayout) findViewById(R.id.fragment);
    mFragmentTransaction = getSupportFragmentManager().beginTransaction();
    mFragmentTransaction.add(mFragmentHolder.getId(), mFragmentTafferTime, TAG);
    mFragmentTransaction.addToBackStack(TAG);
    mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    mFragmentTransaction.commit();
  } 

// On fragment change try to use this function
private void changeFragment(SherlockFragment fragment, String tag) {
    try {
        mFragmentTransaction.replace(mFragmentHolder.getId(), fragment, tag);
        mFragmentTransaction.addToBackStack(tag);
        mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        mFragmentTransaction.commit();
    } catch (Exception exception) {

    }
}
like image 160
Usman Afzal Avatar answered Nov 12 '22 12:11

Usman Afzal