我有Activity托管的幻灯片菜单包含片段,当用户点击slidemenu片段中的任何项目时,我需要处理后退导航
当用户点击列表中的项目时,我调用此方法显示正确的片段
public void SelectItem(int position) {
Fragment _fragment = null;
switch (position) {
case 0:
_fragment = Test_Home.newInstance();
break;
case 1:
_fragment = Diseases_Fragment.newInstance();
break;
........
case 8:
Logout();
break;
default:
break;
}
if (_fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.view_content, _fragment).addToBackStack(null).commit();
// set title , this textview in toolbar
_frag_title.setText(nav_items[position]);
mDrawerLayout.closeDrawer(GravityCompat.START);
}
此方法处理后退按钮
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
我的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_view);
Toolbar _toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
_frag_title = (TextView) _toolbar.findViewById(R.id.frag_title);
}
实际上它工作得很好,但我的问题是显示片段显示旧标题不显示新标题
有方法调用我的方法_frag_title.setText(nav_items[position]);
我的问题不在后面导航,我的问题是如何在导航中更新工具栏标题
答案 0 :(得分:6)
每当更改片段时,都要更改工具栏的标题。
要使用TAG,你必须添加片段TAG,每当你在布局中添加片段时,让我们通过TAG添加片段
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.replace(R.id.MainFrameLayout,fragmentA,"YOUR_TARGET_FRAGMENT_TAG")
.addToBackStack("YOUR_SOURCE_FRAGMENT_TAG").commit();
执行上述操作后,在 onBackPressed 中检查您所在的片段,使用以下代码
@Override
public void onBackPressed(){
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
Log.i("MainActivity", "popping backstack");
fm.popBackStack();
Fragment fragment = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
if (fragment instanceof FragmentA) {
// add your code to change title of toolbar
}
} else {
Log.i("MainActivity", "nothing on backstack, calling super");
super.onBackPressed();
}
}
答案 1 :(得分:1)
解决此问题后,这是我的一段代码
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
UserControl2 UC2 = new UserControl2();
UserControl3 UC3 = new UserControl3();
UserControl4 UC4 = new UserControl4();
public Form1()
{
InitializeComponent();
Controls.Add(UC1);
Controls.Add(UC2);
Controls.Add(UC3);
Controls.Add(UC4);
}
private void ShowUserControl1_Click(object sender, EventArgs e)
{
UC1.Visible = true;
UC2.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl2_Click(object sender, EventArgs e)
{
UC2.Visible = true;
UC1.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl3_Click(object sender, EventArgs e)
{
UC3.Visible = true;
UC1.Visible = false;
UC2.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl4_Click(object sender, EventArgs e)
{
UC4.Visible = true;
UC3.Visible = false;
UC2.Visible = false;
UC1.Visible = false;
}
}
我在public void SelectItem(int position) {
Fragment _fragment = null;
switch (position) {
case 0:
_fragment = Test_Home.newInstance();
break;
case 1:
_fragment = Diseases_Fragment.newInstance();
break;
........
.....
case 8:
Logout();
break;
default:
break;
}
if (_fragment != null) {
// I add tag when I add fragment to BackStack
String backStackName = fragment.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.view_content, fragment, "TARGET_TAG")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(backStackName)
.commit();
_frag_title.setText(nav_items[position]);
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}
方法
onCreate
答案 2 :(得分:0)
你必须在片段的每个片段的恢复时处理这个,以便在片段返回时指定标题。
目前发生的情况是,您在onCreate
或其实际创建fragment
的某个地方设置标题,但由于tool bar
不是位于Activity
内的内容}(不在Fragment
中)您必须对此进行具体更改。最佳位置是片段的onResume
方法。
因此,请尝试在TextView
的onResume
中设置Fragment