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.
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