InterviewSolution
Saved Bookmarks
| 1. |
You’re replacing one Fragment with another — how do you ensure that the user can return to the previous Fragment, by pressing the Back button? |
|
Answer» Android OS provides BACK stack function for Activity, it also provides back stack function for Fragment. If you add ONE Fragment into the back stack, when you PRESS the android device back menu, you can find the Fragment that is saved in back stack popup. Until all saved Fragments in back stack popup, then the activity will exit. We have to create “FragmentTransaction” objects on the back stack and when the user presses the Back BUTTON, the “FragmentManager” pops the most recent transaction off the back stack and performs the reverse action. As mentioned in below code: getSupportFragmentManager().beginTransaction() .add(detailFragment, "DETAIL") // Add this transaction to the back stack .addToBackStack(null) .commit(); |
|