使用片段/工具栏返回导航 [英] Back navigation with Fragments / Toolbar
问题描述
我现在正在抓这个。....我有一个 ActionBarActivity
,它会加载初始的 Fragment
-活动中原始菜单被放大。现在,我有一个导航栏,当选择一个项目时,它会加载一个不同的片段并将其添加到堆栈中。
I'm scratching my head with this one now.... I have an ActionBarActivity
that loads an initial Fragment
- the original menu is inflated within the activity. Now, I have a navigation bar that, when an item is selected, loads a different fragment and adds this to the backstack.
当我这样做时,有几个我要设置的内容:
When I do this, there are a couple of things I want to set:
- 将房屋设置为向上指示器
- 使选项菜单无效主要活动中的
- 设置片段的选项为true
- 确保向上指示器正确导航回到原始片段
- Set the home as up indicator
- Invalidate the options menu from the main activity
- Set has options to true for the Fragment
- Ensure that the up indicator correctly navigates back to the original Fragment
有些奇怪的事情正在发生-向上指示器仅出现一次,不用作后退按钮,尽管我已经使新的按钮无效并膨胀了菜单,新菜单会附加到原始活动菜单。
Something rather strange is going on - the up indicator appears once only and does not behave as the back button and although I've invalidated and inflated a new menu, the new menu gets appended to the original Activity menu.
编辑:好,我已经解决了附加问题-忘记在 onCreateOptionsMenu
方法中添加 menu.clear()
。
Ok I've resolved the appending issue - forgot to add menu.clear()
in the onCreateOptionsMenu
method.
我的导航抽屉布局的所有菜单都有 onClick
个方法会触发另一个片段加载的项目:
My navigation drawer layout has onClick
methods to all menu items which would trigger the load of another Fragment:
public void navItemClick(View view) {
switch (view.getId()) {
case R.id.ripSMS:
mNavigationDrawer.toggleHome(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FragmentTransaction mTrans = getSupportFragmentManager().beginTransaction();
mTrans.replace(R.id.voiceover_frame_layout,new MessageFragment(),"main_ui")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).addToBackStack("msg").commit();
break;
case R.id.ripEmail:
break;
case R.id.ripSettings:
break;
}
mNavigationDrawer.closeDrawer();
}
toggleHome:
toggleHome:
public void toggleHome(boolean show) {
mDrawerToggle.setDrawerIndicatorEnabled(show);
if (!show) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
一旦触发该项目, onCreate
包含invalidate和hasOptions代码:
Once the item is triggered the onCreate
contains the invalidate and the hasOptions code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().invalidateOptionsMenu();
setHasOptionsMenu(true);
}
onCreateOptionsMenu
然后
如前所述,这仅部分起作用一次-第一次使用该项目加载Fragment时,我得到后退图标,但它也不起作用(这在 onOptionsItemSelected
中设置,以检查是否按了主页项-它什么也不做)。当我按下返回按钮时,它将带我回到正确的位置。但是,如果我向后浏览,即使代码通过 onCreate
!
As mentioned, this only partially works once - the first time I use the item to load the Fragment, I get the back icon but it's also not working (this is set within onOptionsItemSelected
to check for the home item press - it does nothing). When I press the back button it takes me back to the correct place. If I navigate back however, the back arrow now longer shows even though the code runs through onCreate
!
推荐答案
好,所以经过一些反复尝试,我设法解决了这个问题。进行了两项更改:
Ok so I managed to solve this after some trial and error. Two changes made:
- 实现
addOnBackStackChangedListener
-
ActionBarDrawerToggle的
setToolbarNavigationClickListener
需要设置
- Implement
addOnBackStackChangedListener
ActionBarDrawerToggle's
setToolbarNavigationClickListener
needed to be set
由于我只有一个活动(所有其他都是Fragment类),所以我将Backstack侦听器添加到了父活动的 onCreate
方法中:
As I only have one activity (everything else is Fragment classes) I added the backstack listener to the Parent Activity's onCreate
method:
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
});
这样可以解决返回片段时消失的后退箭头。最后,将侦听器添加到我的NavigationDrawer的安装程序类中:
This resolved the disappearing back arrow when going back to the fragment. Finally added the listener to my NavigationDrawer's setup class:
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
我想我唯一的问题是使用 onOptionsItemSelected
方法和 android.R.id.home
,但这对我没用。当然,这也许就是我实施的方式,但是如果有人对为什么出现任何明显的事情,请告诉我!
I suppose the only questions I have is everything pointed towards using the onOptionsItemSelected
method with android.R.id.home
but this never worked for me. It might be the way I've implemented things of course but if someone sees anything obvious as to why please do let me know!
这篇关于使用片段/工具栏返回导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!