当我在我的应用程序中按下一个按钮时,我需要返回到最后一个活动。
有什么想法吗?
发布于 2014-04-15 20:33:36
仅供记录:在某些情况下,所描述的方法与back按钮的功能不同,但您可以调用
this.onBackPressed();
或
getActivity().onBackPressed();
如果你在一个片段中实现完全相同的行为。
发布于 2013-05-10 03:07:07
使用片段时:
getFragmentManager().popBackStack();
或
getSupportFragmentManager().popBackStack();
如果您使用的是android.support.v4.app包
发布于 2014-02-03 19:23:55
这适用于以下情况:同一片段有时可能是活动中的唯一片段,有时是多片段活动的一部分,例如在两个片段同时可见的平板电脑上。
/**
* 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();
}
}
例如,可以调用此代码来实现取消按钮。
if (theButton.getId() == R.id.btnStatusCancel) {
StaticMethods.fragmentImplementCancel(this);
}
https://stackoverflow.com/questions/2717954
复制相似问题