【发布时间】:2015-09-28 20:38:23
【问题描述】:
我最近将我的应用程序从原生 Fragment 切换到 v4 支持 Fragment 库,但是现在当我将 Fragment 从返回堆栈中弹出时,不会在前一个 Fragment 上调用 onCreateView()。替换片段时,我需要能够更改标题中的按钮。我尝试同时使用 onHiddenChanged() 和 setUserVisibleHint() 但片段重新出现时似乎都没有被调用。
阅读另一个线程,我看到人们说要使用 onBackStateChanged 侦听器,但我遇到了一些问题。当我的应用程序启动时,它会用文章(部分)的列表视图替换片段容器。当用户选择一篇文章时,它会将部分片段替换为文章片段。记录返回堆栈的计数现在为 1。当用户点击返回按钮时,剖面视图再次显示。我希望能够为我的部分片段调用 onResume 但计数为 0 并说:
09-28 00:45:17.443 21592-21592/com.reportermag.reporter E/Backstack 大小:0 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v4.app.Fragment.onResume()'
如何获得对文章列表片段的引用,以便调用 onResume()?
我试过的代码:
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();
}
【问题讨论】:
-
是否也将片段添加到 backStack 中?
-
很抱歉也应该发布该代码。我确实将它添加到后台。在我的 Main onCreate 中,它调用 loadSectionFragment()。 Backstack 计数仍然为 0,但之后似乎。
-
而不是 replace(containerId, fragment) 你能用 add(containerId, fragment) 检查吗.. 我认为这是问题..
标签: android android-fragments android-support-library onresume