gpt4 book ai didi

android - 如何在不恢复中间 fragment 的情况下清除 Android fragment 返回堆栈

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:38 25 4
gpt4 key购买 nike

我需要清除返回堆栈中的所有 fragment 。

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

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

我试过很多方法:

@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() 时 fragment 仍在处理其生命周期方法。当 fragment 暂停时,为其中一个 fragment 检索配置文件数据的 AsyncTask 被取消(带有中断)。但是 fragment 暂停得太晚了。

而且错误并不总是命中。但大多数时候它会在 Samsung 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

最佳答案

试试这个:

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

关于android - 如何在不恢复中间 fragment 的情况下清除 Android fragment 返回堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30739968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com