4

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 ListFragment from the parent FragmentActivity.

However, I'm using the template Swipe + Fixed Tabs generated by eclipse. I tried calling getSupportFragmentManager().findFragmentById(R.id.myfragment) but the return value is always null. I'm guessing this might be a problem because I did not define myfragment anywhere in my app. But I am unsure where to define it as all fragments are created dynamically.

For those who are not familiar with the Swipe + Fixed Tabs template generated by the Android SDK in Eclipse, the fragments are created by overriding the FragmentPagerAdapter getItem function, returning a new instance of my fragment.

Any help would be appreciated.

Relevant code: How I setup my adapter:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.activity_comment);
mViewPager.setAdapter(mSectionsPagerAdapter);

Overriding the adapter getItem function:

@Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.

        switch(position) {
        case 0:
            return CommentsFragment.newInstance();

        case 1:
        default:
            return LikesFragment.newInstance();
        }
    }

The newInstance() functions simply return an instance of themselves, since the classes for my fragment are static.

Community
  • 1
  • 1
l3utterfly
  • 2,106
  • 4
  • 32
  • 58
  • you should post relevant snippet of code. All the same, if I have not misunderstood your issue, you can try to assign a tag to the fragment a try to retrieve it with findFragemtByTag – Blackbelt Oct 30 '13 at 08:31
  • Updated question with relevant code. – l3utterfly Nov 01 '13 at 04:01

4 Answers4

9

You have to do this :

frag.getFragmentManager().beginTransaction().detach(frag).commit();
frag.getFragmentManager().beginTransaction().attach(frag).commit();
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
8

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();
XorOrNor
  • 8,868
  • 12
  • 48
  • 81
  • 1
    I'm not sure how to implement your method.. I've updated the question with the code I have. Apparently, I did not use fragmentTransaction to add the fragment to the manager. – l3utterfly Nov 01 '13 at 04:02
  • 1
    It's not working. Error happened: "Can't change tag of fragment". Please see my related question: http://stackoverflow.com/questions/24833912/refresh-fragment-ui-from-fragmentactivity – Dr.jacky Jul 19 '14 at 08:03
  • use getSupportFragmentManager() if your app supporting v4. – Prakash Jul 21 '16 at 13:36
  • Is it the only way? Why don't save the fragment instance in an attribute of the activity class? – JCarlosR Aug 11 '16 at 20:13
3

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.

luckyhandler
  • 10,651
  • 3
  • 47
  • 64
1

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