如何清除Android片段后备栈而不恢复中间片段

时间:2015-06-09 18:26:39

标签: android android-fragments

我需要清除后堆栈中的所有碎片。

问题是我需要确保在某个时间点,堆栈已成功清除,之后不会恢复任何弹出的片段。

某些片段显示与当前登录用户相关的内容。 现在,如果用户决定退出,我想清除堆栈,注销用户并将应用程序转换为初始片段。 问题是,堆栈弹出是延迟的,在我退出后,一些用户相关的片段尝试恢复并询问用户配置文件数据。如果没有登录用户,则底层API会引发异常。因此片段崩溃,因为它不应该在这种状态下使用。

我尝试了很多方法:

@Override
public void onSignOutConfirmed() {
    Log.d(TAG, "onSignOutConfirmed");

    getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    Log.d(TAG, "onSignOutConfirmed - signing out");

    UserProfileService.signOut();
    Analytics.trackEvent(EventType.SIGN_OUT);
    showStartupScreen();
}

@Override
public void onSignOutConfirmed() {
    Log.d(TAG, "onSignOutConfirmed");

    FragmentManager fm = getSupportFragmentManager();

    int backStackCount = fm.getBackStackEntryCount();

    while(backStackCount-- != 0) {
        fm.popBackStackImmediate();
    }

    Log.d(TAG, "onSignOutConfirmed - signing out");

    UserProfileService.signOut();
    Analytics.trackEvent(EventType.SIGN_OUT);
    showStartupScreen();
}

日志显示,当我调用UserProfileService.signOut()时,片段仍在处理其生命周期方法。当片段暂停时,取消(使用中断)取消其中一个片段的配置文件数据的AsyncTask。但片段暂停太晚了。

错误并不总是如此。但大部分时间都是在三星S2(Android 4.1.2)上。

06-09 19:46:54.760 D/com.myapp.application.MainActivity( 4562): **onSignOutConfirmed**
06-09 19:46:54.765 D/com.myapp.application.screens.main.MainFragment( 4562): onCreateView
06-09 19:46:54.826 D/com.myapp.application.screens.main.MainFragment( 4562): onCreateAnimation (transit 0, enter true, nextAnim 0)
06-09 19:46:54.831 D/com.myapp.application.screens.main.TranscriptionsFragment( 4562): onCreateView
06-09 19:46:54.856 D/com.myapp.application.screens.main.MainFragment( 4562): onResume
06-09 19:46:54.856 D/com.myapp.application.screens.main.TranscriptionsFragment( 4562): **onResume**
06-09 19:46:54.866 D/com.myapp.application.MainActivity( 4562): **onSignOutConfirmed - signing out**
06-09 19:46:54.876 D/myappDB( 4562): Application settings: id 1.
06-09 19:46:54.876 D/myappDB( 4562): Application settings (id 1): updated, [_activeProfileId] = [null].
06-09 19:46:54.916 D/myappDB( 4562): Application settings: active user profile null.
06-09 19:46:54.941 D/myappDB( 4562): Application settings: active user profile null.
06-09 19:46:54.951 D/com.myapp.application.screens.main.TranscriptionsFragment( 4562): **onPause**
06-09 19:46:54.951 D/com.myapp.application.screens.main.MainFragment( 4562): onPause
06-09 19:46:54.961 D/com.myapp.application.screens.main.TranscriptionsFragment( 4562): onDestroyView
06-09 19:46:54.961 D/com.myapp.application.screens.main.MainFragment( 4562): onDestroyView
06-09 19:46:54.961 D/com.myapp.application.screens.main.MainFragment( 4562): onCreateAnimation (transit 0, enter false, nextAnim 0)
06-09 19:46:55.381 E/AndroidRuntime( 4562):     at com.myapp.application.impls.sqlite.SQLiteApplicationSettingsPersistance.getAndCheckActiveUserProfileId(SQLiteApplicationSettingsPersistance.java:421)
06-09 19:46:55.381 E/AndroidRuntime( 4562):     at com.myapp.application.impls.sqlite.SQLiteTranscriptionPersistance.getTranscriptionStats(SQLiteTranscriptionPersistance.java:54)
06-09 19:46:55.381 E/AndroidRuntime( 4562):     at com.myapp.application.impls.finders.TranscriptionListFinder.getTranscriptions(TranscriptionListFinder.java:45)
06-09 19:46:55.381 E/AndroidRuntime( 4562):     at com.myapp.application.screens.main.TranscriptionsFragment$RetrieveTranscriptionsAsyncTask.doInBackground(TranscriptionsFragment.java:244)
06-09 19:46:55.381 E/AndroidRuntime( 4562):     at com.myapp.application.screens.main.TranscriptionsFragment$RetrieveTranscriptionsAsyncTask.doInBackground(TranscriptionsFragment.java:241)
06-09 19:46:55.421 D/com.myapp.application.screens.main.TranscriptionsFragment( 4562): onDetach

1 个答案:

答案 0 :(得分:-1)

试试这个:

private void clearFragmentBackStack() {
    FragmentManager fm = getSupportFragmentManager();
    for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
        fm.popBackStack();
    }
}