MainActivity初始化和监听返回键
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getSupportFragmentManager();
addFragment(new LoginFragment(), "login");
}
public void addFragment(Fragment fragment, String tag) {
FragmentTransaction beginTransaction = mFragmentManager.beginTransaction();
beginTransaction.add(R.id.login_content, fragment, tag);
// 添加到回退栈,并定义标记
beginTransaction.addToBackStack(tag);
beginTransaction.commit();
}
/**
* 监听返回键
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// 获取当前回退栈中的Fragment个数
int backStackEntryCount = mFragmentManager.getBackStackEntryCount();
// 回退栈中至少有多个fragment,栈底部是首页
if (backStackEntryCount > 1) {
// 回退一步
mFragmentManager.popBackStackImmediate();
// 获取当前退到了哪一个Fragment上,重新获取当前的Fragment回退栈中的个数
FragmentManager.BackStackEntry backStack = mFragmentManager.getBackStackEntryAt(mFragmentManager.getBackStackEntryCount() - 1);
// 获取当前栈顶的Fragment的标记值
String tag = backStack.getName();
if (tag.equals("login")) {
if (isLogin) {
addFragment(new LoginFragment(), "login");
isLogin = false;
} else
finish();
}
} else {
finish();
}
}
return true;
}