Hi Currently am developing application using fragments.Totally i have 15 fragments and loaded it in single Activity.While onBack Pressing on Each fragment will launch previous fragment.I google about it and also i got answers for it.but i just want to know which is more efficient way handle this.
Asked
Active
Viewed 1,552 times
2
-
1**While onBack Pressing on Each fragment will perform different actions**. What actions? Back button will take you back to previous fragment. I also don't understand the need for 15 fragments hosted by 1 activity. – Raghunandan Aug 19 '15 at 11:44
-
@Raghunandan previous fragments. – IND VISA Aug 19 '15 at 11:46
-
Each fragments are loaded on another fragment onClick – IND VISA Aug 19 '15 at 11:46
-
1ok. What is that you are looking for? – Raghunandan Aug 19 '15 at 11:47
-
add all fragment in `addToBackStack(null)` while transaction – N J Aug 19 '15 at 11:47
-
http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/ – IntelliJ Amiya Aug 19 '15 at 11:48
-
i have an doubt if we are doing like this..While on serveral conditions(i.e)if GC removes the backstack how can we get it. – IND VISA Aug 19 '15 at 11:49
-
what this code can do:addToBackStack(null). – IND VISA Aug 19 '15 at 11:51
1 Answers
2
By calling addToBackStack()
, the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button. Reference
If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack()
, then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.
Just add this transaction.addToBackStack(null);
For more information you may visit Handling back button press Inside Fragments

Community
- 1
- 1

IntelliJ Amiya
- 74,896
- 15
- 165
- 198