我最近将我的应用程序从本机片段切换到v4支持片段库,但现在当我从后面的堆栈弹出片段onCreateView()时,不会在前一个片段上调用。更换片段时,我需要能够更改标题中的按钮。我试图同时使用onHiddenChanged()和setUserVisibleHint(),但是当片段重新进入视图时似乎都没有调用。
读另一个帖子,我看到有人说要使用onBackStateChanged监听器,但我遇到了一些问题。当我的应用程序启动时,它会用一个文章列表视图替换一个片段容器(部分)。当用户选择文章时,它会用文章片段替换部分片段。现在记录后栈的计数为1.当用户点击后退按钮时,将再次显示剖面视图。我希望能够为我的节片段调用onResume,但计数为0并说:
09-28 00:45:17.443 21592-21592 / com.reportermag.reporter E / Backstack size:0 java.lang.NullPointerException:尝试调用虚方法' void android.support.v4.app.Fragment.onResume()'在空对象引用上
如何获取文章列表片段的引用,以便我可以调用onResume()?
Code I尝试过:
public void onBackStackChanged() {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
if (manager != null)
{
int backStackEntryCount = manager.getBackStackEntryCount();
Log.e("Backstack size", Integer.toString(backStackEntryCount));
android.support.v4.app.Fragment fragment = manager.getFragments().get(backStackEntryCount > 0 ? backStackEntryCount-1:backStackEntryCount);
fragment.onResume();
}
}
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
// Set the titlebar
Titlebar.setColor(getResources().getColor(R.color.graydark));
Titlebar.setVisible(Titlebar.VIEWS.MENU, Titlebar.VIEWS.LOGO, Titlebar.VIEWS.SEARCH);
// Clear Search
SearchFragment.clearSearch();
}
}
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(hidden == false) {
// Set the titlebar
Titlebar.setColor(getResources().getColor(R.color.graydark));
Titlebar.setVisible(Titlebar.VIEWS.MENU, Titlebar.VIEWS.LOGO, Titlebar.VIEWS.SEARCH);
// Clear Search
SearchFragment.clearSearch();
}
}
更新
这是我的片段加载器:
public void loadSectionFragment(Integer sectionID) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Set the arguments
Bundle bundle = new Bundle();
bundle.putInt("section", sectionID);
// Add the section fragment
Fragment sectionFrag = sections.get(sectionID);
if (sectionFrag == null) {
sectionFrag = new SectionFragment();
sectionFrag.setArguments(bundle);
sections.put(sectionID, sectionFrag);
}
transaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim);
transaction.replace(R.id.fragment_container, sectionFrag);
transaction.addToBackStack(null);
// Commit the new fragment
transaction.commit();
}
public void loadArticleFragment() {
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
// Set the arguments
Bundle bundle = new Bundle();
bundle.putInt("id", id);
bundle.putInt("color", color);
// Add the article fragment
Fragment articleFrag = new ArticleFragment();
articleFrag.setArguments(bundle);
transaction.replace(R.id.fragment_container, articleFrag);
transaction.addToBackStack(null);
// Commit the new fragment
transaction.commit();
}
答案 0 :(得分:1)
如果从Backstack返回时需要更新片段,请使用以下模式:
backStackListener = new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.yourFragmentContainerId);
if(f!=null){
if(f instanceof YourSectionFragment ){
((YourSectionFragment )f).update();
}else{
}
}
}
};
getSupportFragmentManager().addOnBackStackChangedListener(backStackListener);
然后将方法添加到片段
public void update(){
//update your ui
}