I have one Activity with five fragments. By default the first fragment is displayed when the user clicks on Submit, then the second fragment is displayed. However I don't understand how to get back to the previous fragment.
Asked
Active
Viewed 60 times
1 Answers
0
try this,
add this code in your activity.
Fragment fragment = null;
switch (menu.get(position).name) {
case "Board": {
progressBar.setVisibility(View.VISIBLE);
fragment = new TabHostFragment(); // your fragment
break;
}
case "Test": {
fragment = new ScheduledTestsFragment(); // your fragment
break;
}
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}

Gayathri
- 249
- 3
- 13
-
what should i add in fragment when back button is pressed – Ext1 Dev Oct 29 '18 at 11:23
-
you should add this code only in your activity. not in fragment. – Gayathri Oct 29 '18 at 11:26
-
then how the back button will know on which fragment he has to go? – Ext1 Dev Oct 29 '18 at 12:17
-
assume that.are you in activity. then go 1st fragment , next go to 2nd fragment, next go to 3rd fragment. now you are in 3rd fragment. Then click back button the 2nd fragment will be view. – Gayathri Oct 29 '18 at 12:41