gpt4 book ai didi

android - 从菜单项中选择一个是要进行的 Activity ,我怎样才能在 android 中直接进行 Activity

转载 作者:行者123 更新时间:2023-11-29 19:26:01 25 4
gpt4 key购买 nike

Activity main 包含在 cade 下面,一切正常,但是当我从抽屉注销时,我想去 Activity instand fragment,如果我放 break;如果注销比 fragment 类不解决。

 public void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
Float elevation = getResources().getDimension(R.dimen.elevation_toolbar);

switch (menuItem.getItemId()) {

case R.id.home:
fragmentClass = HomeFragment_.class;
break;
case R.id.process:
fragmentClass = ProcessFragment_.class;
break;
case R.id.decline:
fragmentClass = RejectFragment_.class;
break;

case R.id.more:
try {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/search?q=mndigital")));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/search?q=mndigital")));
}
fragmentClass = HomeFragment_.class;
break;
case R.id.logout:
VPreferences.reset();
Intent intent = new Intent(MainActivity.this,LoginActivity_.class);
startActivity(intent);
finish();
// fragmentClass = HomeFragment_.class;
// break;
default:
fragmentClass = HomeFragment_.class;
break;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();


menuItem.setChecked(true);
setTitle(menuItem.getTitle());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
toolbar.setElevation(0.0f);

drawerLayout.closeDrawers();
}

当我从抽屉登录 Activity 中单击注销时,打开我如何到达。

最佳答案

您需要在 case R.id.logout: 之后放置一个break

当 case 是 R.id.logout 时,它也会转到 default case。

编写 finish() 不会立即终止 Activity ,而是在完成之前完成该线程中的其他未决指令。

或者您可以在启动 LoginActivity_ 之后,从开关 block 返回控制流。

case R.id.logout:
VPreferences.reset();
Intent intent = new Intent(MainActivity.this,LoginActivity_.class);
startActivity(intent);
finish();
break; // to make sure it doesn't go in the default block
return; // to return the flow of control outside the switch statement

关于android - 从菜单项中选择一个是要进行的 Activity ,我怎样才能在 android 中直接进行 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com