当前位置: 首页 > 知识库问答 >
问题:

popBackStack后未还原ViewPager中的片段

卞博简
2023-03-14

只要不按后退键,一切都很好。一旦用户关闭其中一个子页面列表,就会重新创建以前的列表,但没有以前显示的页面。在父页面列表中浏览其他页面仍然有效。

在GitHub上可以找到一个示例应用程序

public class MainActivity extends FragmentActivity {

private static final String CURRENT_FRAGMENT = MainActivity.class.getCanonicalName() + ".CURRENT_FRAGMENT";

public static final String ARG_PARENTS = "Parents";

public void goInto(String mHostingLevel, String mPosition) {
    Fragment hostingFragment = newHostingFragment(mHostingLevel, mPosition);
    addFragment(hostingFragment);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addBaseFragment();
}

private void addBaseFragment() {
    Fragment hostingFragment = newHostingFragment("", "");
    addFragment(hostingFragment);
}

private Fragment newHostingFragment(String mHostingLevel, String oldPosition) {
    Fragment hostingFragment = new PageListFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARENTS, mHostingLevel + oldPosition +" > ");
    hostingFragment.setArguments(args);
    return hostingFragment;
}

private void addFragment(Fragment hostingFragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragmentSpace, hostingFragment, CURRENT_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}

}
public class PageListFragment extends Fragment {

private String mParentString;

public PageListFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_hosting, container, false);
}

@Override
public void onResume() {
    mParentString = getArguments().getString(MainActivity.ARG_PARENTS);
    ViewPager viewPager = (ViewPager) getView().findViewById(R.id.viewPager);
    viewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getFragmentManager(),mParentString));
    super.onResume();
}

private static class SimpleFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

    private String mHostingLevel;

    public SimpleFragmentStatePagerAdapter(FragmentManager fm, String hostingLevel) {
        super(fm);
        this.mHostingLevel = hostingLevel;
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        PageFragment pageFragment = new PageFragment();
        Bundle args = new Bundle();
        args.putString(MainActivity.ARG_PARENTS, mHostingLevel);
        args.putInt(PageFragment.ARG_POSITION, position);
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public int getCount() {
        return 5;
    }
}
}
public class PageFragment extends Fragment {

public static final String ARG_POSITION = "Position";

private String mHostingLevel;
private int mPosition;

public PageFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View contentView = inflater.inflate(R.layout.fragment_page, container, false);
    setupTextView(contentView);
    setupButton(contentView);
    return contentView;
}

private void setupTextView(View contentView) {
    mPosition = getArguments().getInt(ARG_POSITION);
    mHostingLevel = getArguments().getString(MainActivity.ARG_PARENTS);
    TextView text = (TextView) contentView.findViewById(R.id.textView);
    text.setText("Parent Fragments " + mHostingLevel + " \n\nCurrent Fragment "+ mPosition);
}

private void setupButton(View contentView) {
    Button button = (Button) contentView.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openNewLevel();
        }
    });
}

protected void openNewLevel() {
    MainActivity activity = (MainActivity) getActivity();
    activity.goInto(mHostingLevel, Integer.toString(mPosition));
}

}

共有1个答案

姚高韵
2023-03-14

经过长时间的调查,原来是片段管理器的问题。

当使用类似上面的构造时,片段事务将片段重新附加到页面列表上,则会静默地丢弃该片段。基本上是同样的问题导致

java.lang.IllegalStateException: Recursive entry to executePendingTransactions 

当试图更改FragmentPager中的片段时。

    viewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getFragmentManager(),mParentString));
    viewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getChildFragmentManager(),mParentString));
 类似资料:
  • 我对片段有问题,或者更确切地说是我希望它们工作的方式有问题。 在我的活动中,我有一个布局容器(R.id.container1),我在启动时以编程方式向其中添加一个片段(FragMain)。在片段中,我有2个片段(Frag1、Frag2)和一个ViewPager,它通过FragmentPagerAdapter加载其他几个片段(FragA、FragB、FragC、FragD、FragE)。 按下一个按

  • 可以使用保存删除片段时的状态,然后在将片段弹出后堆栈时恢复状态吗?当我从后堆栈还原片段时,savedInstanceState包始终为空。 现在,应用程序流程是:创建的片段->删除的片段(添加到后堆栈)->从后堆栈恢复的片段(savedInstanceState包为空)。 下面是相关代码: 我认为问题是在被移除和添加到后堆栈时从未被调用。如果我不能使用onsavedInstanceState(),

  • 我似乎不明白为什么在设置适配器后片断是空的,但是如果我把片断更新代码放在单击事件中,我就没有问题了。

  • 我有一个有4个选项卡的ViewPager片段,在第4个选项卡中有另一个有3个选项卡的ViewPager,这样,如果每当我切换我的topviewPagerTab(一个有4个选项卡)时,就不会调用sub ViewPager(有3个选项卡)中的setUserVisiblehint片段。

  • 我找到的唯一解决办法就是打电话给 在显示父片段之后。这会以某种方式触发视图被重新创建并出现在屏幕上。不幸的是,这有时会导致在旧的观察器上处理两次调用的寻呼机适配器时出现异常。 有没有更好的办法做到这一点?我想我要问的是:

  • 我正在考虑一个简单的例子,包括以下部分: Main活动:有编辑文本、按钮、viewPager 碎片A:什么都没有 碎片B:有文本视图 所以,在屏幕上,你可以滑动查看每个片段。我希望我的例子有这样一个功能:如果一个人在editText中写了一个文本,然后点击按钮,它就会出现在片段B中。 A使用notifyDatsetChanged()尝试了它,但不起作用。有人能帮忙吗?下面是我的全部代码: 主要活动