我有一个如下图所示开发的应用程序:
因此带有菜单的片段A。单击菜单将打开片段B。 我的问题是,当我单击“后退”按钮时,什么也没发生,因此我无法从片段B返回片段A,而且我也不明白为什么。
这是我的代码:
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
drawer.openDrawer(Gravity.LEFT);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FragmentA fragmentA = new FragmentA();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativelayout_for_fragment, fragmentA, fragmentA.getTag()).commit();
getSupportActionBar().setTitle(getResources().getString(R.string.title));
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if(getSupportFragmentManager().getBackStackEntryCount() == 0){
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
toggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}else{
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
toggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
drawer.openDrawer(GravityCompat.START);
} else {
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
片段A(在菜单上单击)
Fragment fragmentB = new FragmentB();
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
transaction.replace(R.id.relativelayout_for_fragment, fragmentB);
transaction.addToBackStack(null);
transaction.commit();
片段B
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.share_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getFragmentManager().popBackStack();
return true;
case R.id.menu_item_share:
String contenuto = "*" + titolo + "*" + "\n" + desc;
String textToShare = getResources().getString(R.string.Linc);
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.putExtra("android.intent.extra.TEXT", contenuto + "\n\n" + textToShare);
startActivity(Intent.createChooser(intent,
getResources().getString(R.string.sharing)));
return true;
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:0)
在MainActivity中
之后
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
放置
getSupportFragmentManager().popBackStack()
答案 1 :(得分:0)
您尝试过此config.AWSSecretKey
onBackPressed