安卓app主页面为 FragmentActivity,其余页面为fragmnet页面。
主页面 底部导航栏 三个按钮。 点击三个按钮分别跳转不同的 frgment .如果 点击这个三个按钮时,其中两个或者三个再者一个 , 需要 判断 用户是否登录,(当然跳转的登录页面也是 frgment页面)。那么 跳转到登录页面 ,登录成功后 如何返回
主页面 按钮 对应的 fragment 呢? 这里为大家讲解一下 解决方案!当然这仅仅是针对于 本人项目的解决 方法。不过可能对你 有用哦。
首页先 贴出 主页面 FragmentActivity 里面 判断的代码:
private void eventUser() {
// TODO Auto-generated method stub
buttonChange(2);
if (MyApplication.instance.getUserData() != null) {
changFragment(new UserFragment(), "UserFragment");
} else {
UserLoginFragment mUserLoginFragment = new UserLoginFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Bundle mBundle = new Bundle();
mBundle.putString("user",UserFragment.class.getSimpleName() );
mUserLoginFragment.setArguments(mBundle);
ft.replace(R.id.llfragment, mUserLoginFragment);
ft.addToBackStack("UserTag");
ft.commit();
}
}
private void eventLoan() {
// TODO Auto-generated method stub
buttonChange(1);
if (MyApplication.instance.getUserData() != null) {
changFragment(new LoanFragment(), "LoanFragment");
} else {
UserLoginFragment mUserLoginFragment = new UserLoginFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Bundle mBundle = new Bundle();
mBundle.putString("loan", LoanFragment.class.getSimpleName());
mUserLoginFragment.setArguments(mBundle);
ft.replace(R.id.llfragment, mUserLoginFragment);
ft.addToBackStack("LoanTag");
ft.commit();
}
}
跳转到 登录页面的代码(这里代码看你怎么处理就放在那里):
FragmentManager fm;
FragmentTransaction ft;
String mUser = getArguments().getString("user");
String mLoan = getArguments().getString("loan");
if (UserFragment.class.getSimpleName().equals(mUser)) {
UserFragment mUserFragment = new UserFragment();
fm = getFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.llfragment, mUserFragment);
ft.addToBackStack("UserTag");
ft.commit();
} else if (LoanFragment.class.getSimpleName().equals(
mLoan)) {
LoanFragment mLoanFragment = new LoanFragment();
fm = getFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.llfragment, mLoanFragment);
ft.addToBackStack("LoanTag");
ft.commit();
}
希望帮助到大家。