从细节片段返回 return 到列表片段

Back return from detail fragment to List Fragment

我有一个 ListView,它获取并显示来自 RSS.I 的一些数据,我正在 ListView 上显示,并且使用 onClickListener 我显示列表的详细信息 element.There 没问题,直到 here.But 我的 ListView class 和 detail class 都是 Fragment.When 我想从 detail 返回 List fragment ,它不是 return.How 从 detail 到 List fragment 吗?这里有一些代码;

public class BlogPostsFragment extends Fragment{

...

}

public class BlogDetailFragment extends Fragment{

...

}

这里是Activity中的片段交易;

 private void displayView(int position) {
    fragment = null;
    switch (position) {
    case 0:
        fragment = new BlogPostsFragment();
        break;
    default:
        break;

    }

    if (fragment != null) {
        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(drawerList.get(position));// Burada Fragment'ın titleı
                                            // veriliyor.
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        Log.e("BlogMainPageActivity",
                "Fragment oluşturulurken hata meydana geldi!");
    }
}

这里是代码片段交易;

vi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            BlogDetailFragment bdf = new BlogDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", currentItem.getTitle());
            bundle.putString("description", currentItem.getDesc());
            bundle.putString("pubDate", currentItem.getPubDate());
            bundle.putString("link", currentItem.getLink());

            bdf.setArguments(bundle);

            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, bdf).commit();

        }
    });

我在 activity.So 中开始交易 我从 Activity 调用 BlogPostsFragment。

当您用详细信息片段替换您的列表片段时,您可以将列表片段添加到返回堆栈。这会将列表片段放在返回堆栈的顶部,当用户按下返回时将其弹出。修改您的代码:

vi.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        BlogDetailFragment bdf = new BlogDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", currentItem.getTitle());
        bundle.putString("description", currentItem.getDesc());
        bundle.putString("pubDate", currentItem.getPubDate());
        bundle.putString("link", currentItem.getLink());

        bdf.setArguments(bundle);

        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, bdf).addToBackStack(null).commit();

    }
});

这是您应该在点击时执行的操作:

String fragmentTag = getFragmentTag();
FragmentTransaction fragmentTransaction = FragmentTransaction  fragmentTransaction = getFragmentManager().beginTransaction();
BlogDetailFragment bdf = (BlogDetailFragment) getFragmentManager().findFragmentByTag(fragmentTag);

if(BlogDetailFragment == null) {
    bdf = new BlogDetailFragment();
    //ADD BUNDLE HERE
}
fragmentTransaction.replace(R.id.frame_container, bdf, fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
fragmentTransaction = null;