/**
* Method that can be used by a fragment that has been started by MainFragment to terminate
* itself. There is some controversy as to whether a fragment should remove itself from the back
* stack, or if that is a violation of the Android design specs for fragments. See here:
* http://stackoverflow.com/questions/5901298/how-to-get-a-fragment-to-remove-itself-i-e-its-equivalent-of-finish
*/
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 a Cancel button, for example.
if (theButton.getId() == R.id.btnStatusCancel) {
StaticMethods.fragmentImplementCancel(this);
}