我正在为我的应用使用Android材质导航抽屉,并尝试创建一个退出按钮作为导航抽屉项目之一
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
// Toast.makeText(此,"选择菜单项 - >" +位置,Toast.LENGTH_SHORT).show();
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
在案例9中我想允许用户注销,
Intent logOut = new Intent(GroupHomeActivity.this, LoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
如何在案例9中执行上述代码?
答案 0 :(得分:1)
这就是我认为你应该做的事情:
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
fragment=null;
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
else {
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
只是因为返回类型需要非空片段。好吧,只需返回一个空片段,并在null注销用户。
答案 1 :(得分:1)
以下是我将如何处理第9位点击的逻辑
@Override
public void onNavigationDrawerItemSelected(int position) {
if(position == 9){
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}else{
switchFragment(position);
}
}
以下是切换片段的方法。
private void switchFragment(int position){
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
default:
break;
}
if (fragment != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
答案 2 :(得分:1)
在休息之前,将你的代码放在案例9之后。像这样:
Case 9:
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit(); logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
StartActivity(logout);
break;