处理动作条标题的片段回栈? [英] Handling ActionBar title with the fragment back stack?

查看:120
本文介绍了处理动作条标题的片段回栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,我在加载 ListFragment ,一经点击,它能够深入的水平和一种新型的 ListFragment 的显示,取代了原来的(使用下面的 showFragment 法)。这被放置在背面叠层

在开始的时候,该活动展示了操作栏上的默认标题(即它的设置会自动根据应用程序的安卓标签)。

当表示该列表为层次结构中下一层,该项目的名称点击应成为操作栏的标题

然而,当pressing <大骨节病>返回,我想恢复原来的默认标题。这不是 FragmentTransaction 知道,这样的称号是不可恢复的。

我隐约读到 FragmentBreadCrumbs ,但这似乎需要使用自定义视图。我使用ActionBarSherlock并将preFER不要有我自己的自定义标题视图。

什么是这样做的最好方法是什么?是否有可能没有样板code负载,并具有跟踪,一路上显示的标题?


 保护无效showFragment(片f){
  FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
  ft.replace(R.id.fragment_container,F);
  ft.addToBackStack(空);
  ft.commit();
}
 

解决方案

在每一个片段,每一个活动我改变这样的标题。这样的活动标题永远是正确的:

  @覆盖
公共无效onResume(){
    super.onResume();
    //设置标题
    getActivity()。getActionBar()
        .setTitle(R.string.thetitle);
}
 

I have an Activity where I load in a ListFragment and, upon clicking, it drills down a level and a new type of ListFragment is shown, replacing the original one (using the showFragment method below). This is placed on the back stack.

At the beginning, the activity shows the default title in the action bar (i.e. it's set automatically based on the application's android:label).

When showing the list for the next level in the hierarchy, the name of the item clicked on should become the action bar's title.

However, when pressing Back, I would like the original default title to be restored. This isn't something FragmentTransaction knows about, so the title isn't restored.

I've vaguely read about FragmentBreadCrumbs, but this seems to require using a custom view. I'm using ActionBarSherlock and would prefer to not have my own custom title view.

What is the best way of doing this? Is it possible without a load of boilerplate code and having to keep track of the titles shown along the way?


protected void showFragment(Fragment f) {
  FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  ft.replace(R.id.fragment_container, f);
  ft.addToBackStack(null);
  ft.commit();
}

解决方案

In every fragment and every activity I change the title like this. This way the active title will always be correct:

@Override
public void onResume() {
    super.onResume();
    // Set title
    getActivity().getActionBar()
        .setTitle(R.string.thetitle);
}

这篇关于处理动作条标题的片段回栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆