This is for a situation where the same fragment can sometimes be the only fragment in the activity, and sometimes part of multiple activity, for example, on a tablet, where two fragments are displayed simultaneously.
public static void fragmentImplementCancel(Fragment fragment) { FragmentActivity fragmentActivity = fragment.getActivity(); FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager(); if (fragmentManager.getBackStackEntryCount() == 1) { fragmentManager.popBackStack(); } else { fragmentActivity.finish(); } }
This code can be called to implement the Cancel button, for example.
if (theButton.getId() == R.id.btnStatusCancel) { StaticMethods.fragmentImplementCancel(this); }
RenniePet Feb 03 '14 at 11:23 2014-02-03 11:23
source share