search

Home  >  Q&A  >  body text

android - 如何将activity获得的返回值传递给ViewPager中的fragment?

如何将activity获得的返回值传递给ViewPager中的fragment?

我需要监听返回事件,刷新viewPager中的fragment

ViewPager代码:

public class SectionPagerAdapter extends FragmentPagerAdapter {

        public SectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        //指定fragment
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new MyFriendsFragment();
                case 1:
                    return new MyGroupFragment();
                default:
                    return null;
            }
        }
        //定义列数
        @Override
        public int getCount() {
            return 2;
        }

        //定义列标签
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return "好友";
                case 1:
                    return "群组";
            }
            return null;
        }
    }
    
    需要在activity的onActivityResult方法中传递给fragment,,没法用tag,怎么办还有别的方法吗
黄舟黄舟2772 days ago518

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:34:11

    Activity and Fragment have a parent-child relationship. It is no problem to pass the value directly. However, you directly use the new Fragment here without saving its reference, so it is naturally inconvenient to pass the value. When you save a reference to FragmentPagerAdapter or directly to Activity when you create new, wouldn't you be able to directly call the method in Fragment?
    In addition, if the Activity is called from a Fragment and returned to this Fragment, you can directly use the startActivityForResult and onActivityResult methods in the Fragment. The results have been encapsulated in the Fragment, and the response will be directly passed to this Fragment.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:34:11

    You can try message bus EventBus

    reply
    0
  • Cancelreply