refresh a fragment from its parent activity

后端 未结 4 584
滥情空心
滥情空心 2020-12-06 14:16

I have almost the exact same problem as this question here: Android, How to restart/refresh a fragment from FragmentActivty?

I\'m trying to call the a method of a

相关标签:
4条回答
  • 2020-12-06 14:54

    You have to do this :

    frag.getFragmentManager().beginTransaction().detach(frag).commit();
    frag.getFragmentManager().beginTransaction().attach(frag).commit();
    
    0 讨论(0)
  • 2020-12-06 15:15

    Your fragments are managed by the adapter. Therefore, you also have to call for the adapter to refresh your fragments. These steps are necessary:

    1. Override getItemPosition in your Adapter

      @Override
      public int getItemPosition(Object object) {
          // POSITION_NONE makes it possible to reload the PagerAdapter
          return POSITION_NONE;
      }
      
    2. Once you did that call

      mViewPager.getAdapter.notifyDataSetChanged();
      

      in your Activity. From the fragment object itself you could use a callback to inform the Activity of the need to refresh.

    This will freshly populate the viewPager with new instances of your fragments which is like restarting / refreshing the fragment.

    Hope it helps and this was what you were looking for.

    0 讨论(0)
  • 2020-12-06 15:18

    You can find your fragment by the Tag, but of course you need to give this tag to it while adding the fragment.

    First add your fragment with a tag:

            fragmentManager = getFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            SomeFragment fragment = new ManageLinksFragment();
            fragmentTransaction.add(R.id.fragment_container1,fragment, "sometag");
            fragmentTransaction.commit();
    

    And then on the Activity's site:

    SomeFragment mSomeFragment = (SomeFragment) getFragmentManager().findFragmentByTag("sometag");
    // now mSomeFragment.callsomething();
    
    0 讨论(0)
  • 2020-12-06 15:20

    getFragmentManager().beginTransaction().detach(this).attach(this).commit();

    0 讨论(0)
提交回复
热议问题