1

Dans mon application, je reçois java.lang.IllegalStateException: Impossible d'effectuer cette action après onSaveInstanceState. après onBackPress() d'activité.java.lang.IllegalStateException: Impossible d'effectuer cette action après onSaveInstanceState On Activity Backpress (Ne pas utiliser de fragment)

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(MyApplication:1533) 
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(MyApplication:603) 
     at android.support.v4.app.FragmentActivity.onBackPressed(MyApplication:179) 
     at com.app.mobile.views.NewsActivity.onBackPressed(MyApplication:301) 
     at com.app.mobile.views.NewsActivity.onOptionsItemSelected(MyApplication:293) 
     at android.app.Activity.onMenuItemSelected(Activity.java:2625) 
     at android.support.v4.app.FragmentActivity.onMenuItemSelected(MyApplication:406) 
     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(MyApplication:195) 
     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(MyApplication:103) 
     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(MyApplication:103) 
     at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(MyApplication:183) 
     at android.view.View.performClick(View.java:4446) 
     at android.view.View$PerformClick.run(View.java:18437) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5447) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

Dans mon activité, je n'utilise aucun fragment. J'ai cherché la solution, mais trouvé seulement pour les fragments.

Dans mon code.

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case android.R.id.home: 
     onBackPressed(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

et

@Override 
    public void onBackPressed() { 
    super.onBackPressed(); 
    if (screen_Name != 0 && screen_Name == Constants.TAG_WIDGET_SCREEN) { 
     int tagID = getIntent().getIntExtra(Constants.TAG_ID, -1); 
     int tagcount = getIntent().getIntExtra(Constants.TAG_COUNT, 0); 
     if (tagcount > 0) { 
     mDBAdapter.updateTagCountToZero(tagID); 
     } 
     PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.NEWS + type); 
     PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.CARD_PREF + cardPref); 

    } 
    } 

dans onSavedInstance

@Override 
    protected void onSaveInstanceState(Bundle outState) { 

    if (feedList != null && feedList.size() > 0) { 
     outState.putSerializable(FEED_DATA, feedList); 
    } 
    stopProgressBar(); // stopping progress dialog 
    super.onSaveInstanceState(outState); 
    } 

S'il vous plaît aider comment gérer cette situation.

+0

Je suis juste deviner au hasard, mais avez-vous essayé de mettre '' 'super.onBackPressed()' '' à la fin de votre méthode? peut-être que cela déclenche déjà quelque chose comme onSaveInstanceState – Maximosaic

+0

mais cela n'est pas reproduit. C'est venu en live apk. Donc je ne connais pas la raison exacte. –

Répondre

0

Pour éviter ce problème, vous pouvez vérifier si votre activité est arrivée:

@Override 
public void onBackPressed() { 
    if (!isFinishing()) { 
     super.onBackPressed(); 
     if (screen_Name != 0 && screen_Name == Constants.TAG_WIDGET_SCREEN) { 
      int tagID = getIntent().getIntExtra(Constants.TAG_ID, -1); 
      int tagcount = getIntent().getIntExtra(Constants.TAG_COUNT, 0); 
      if (tagcount > 0) { 
       mDBAdapter.updateTagCountToZero(tagID); 
      } 
      PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.NEWS + type); 
      PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.CARD_PREF + cardPref); 
     } 
    } 
} 
0
Super.onBackPressed() 

invoqueront la fin de votre activité. Si vous voulez terminer l'activité, effectuez d'abord ce qui est nécessaire pour vous, puis appelez ceci à la fin.

+0

Salut, s'il vous plaît voir dans les journaux, exception est en cours à FragmentManagerImpl.checkStateLoss (MyApplication: 1533). –

+0

Je fais juste la tâche liée SharedPreference après super.backPressed(). Si vous voyez dans le flux onOptionsItemSelected() -> onBackPressed -> ... checkStateLoss() et Exception. Donc, je pense que l'exception se produit avant la tâche de préférence. –

+0

Peut-on déplacer l'appel de la méthode super.onBackpressed() à la fin de la méthode onBackPressed()? Comme je l'ai mentionné, super.onBackPressed() déclenchera la fin de l'activité. – 7383