Android Remove Fragment and View from BackStack
-
I realise this question has been asked before however the previous answers have gotten me so far. The scenario is as follows: we have a dashboard fragment (A), which leads a user to a login screen (B). On successful login they go to a listview (c). On backpress I would like to return to A, as the user will not need to see the login screen again. In addition on successful login we store the details in shared preferences and automate the login in B next time, which all works as planned. I have the following FragmentHelper method: public static void goToNextFragement(Fragment fragment, int container, boolean addToBackStack, Fragment ctx) { // Create new fragment and transaction FragmentTransaction transaction = ctx.getSupportFragmentManager().beginTransaction(); transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(container, fragment); if(addToBackStack) transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } In the transaction from B to C I set the Boolean addToBackStack as false so that the transaction.addToBackStack(null); is not called. This again works well but after is where my problem starts. When the user presses back on C and returns to A I can still see the inflated view of C under the view of A. Any help would be appreciated. I hope my diagram helps keep this simple.
-
Answer:
Couple of ways to handle this: I had a similar flow in my app and the way I solved it was by replacing the login fragment with a AlertDialog that's fired from the main activity. So in your case, fragment A shows up on screen and if the main activity things it needs to show the login dialog, it shows the AlertDialog. This worked for me. When fragment A is enabled, it can check if fragment C is around by asking the FragmentManager. If it exists then remove it.
Graham Smith at Stack Overflow Visit the source
Other answers
Not telling the FM that you want the transition from B to C to be added to the backStack means that when you hit back, the FM has no record of adding C, so it doesn't remove it.
Kelly Merrell
Related Q & A:
- How to animate a View with Translate Animation in Android?Best solution by Stack Overflow
- How to replace a fragment with another fragment in Android?Best solution by Stack Overflow
- How to replace a fragment of view pager with other new fragment in android?Best solution by Stack Overflow
- How to add new view at the top of an existing view in Android?Best solution by Stack Overflow
- How to send data from Fragment to an Activity in Android?Best solution by forum.xda-developers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.