I need to clear all fragments from the back stack.
The problem is that I have to make sure that at some point the stack is successfully cleared, and not one of the pop-up fragments will be resumed after.
Some fragments display content that is associated with the currently signed up user. Now, if the user decides to exit, I would like to clear the stack, write the user out, and transfer the application to the source fragment. The problem is that the pop stack is postponed, and after I exit, some user-related fragments try to renew and request user profile data. And the core API throws an exception if it does not have a signed user. Thus, the fragment falls because it should not be used in this state.
I tried many ways:
@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(); }
or
@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(); }
The log shows that the fragment is still processing its lifecycle methods when I call UserProfileService.signOut (). AsyncTask, which retrieves the profile data for one of the fragments, is canceled (interrupted) when the fragment is paused. But the fragment is paused too late.
And the error does not always strike. But most of the time he clicks on the 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