0

How would I add a tag to a newly created instance?

case 0:           
 return FragmentNotifications.newInstance(notifications, mCurrentTime);

The new instance:

    public static FragmentNotifications 
                 newInstance(String notificationsArray, long currentTime){

        FragmentNotifications fragment = new FragmentNotifications();
        return fragment;
    }

How would I assign a name to the fragment created with viewpager?

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return 
             FragmentNotifications.newInstance(notifications, mCurrentTime);
            case 1:
                return FragmentMessages.newInstance(messages, 
              mCurrentTime);
            case 2:
                return new FragmentProfile();
            default:
                return null;

        }

    }
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • What do you mean by a "tag"? You can set a tag value against a Fragment when you load it with the `FragmentManager` - is this what you're referring to? (it looks like `getFragmentManager().beginTransaction().replace(layoutId,frag,"A_TAG").commit();`) – PPartisan May 26 '19 at 20:39
  • Yes but how would I do it using new Instance? Because I'm trying to create a new instance of the fragment inside a viewpager so I can'tt use replace – DIRTY DAVE May 26 '19 at 20:42
  • I don't understand the question - the "tag" is for the `FragmentManager`, as a means of retrieving the fragment later. The Fragment itself has no direct use for it. You can assign tags as constant values in your Fragments, i.e. ..`.replace(layoutId,frag,MyFragment.TAG)...` – PPartisan May 26 '19 at 20:44
  • I'm trying to get the fragment inside the viewpager. But am I sure how to assign it when the fragment is created by the viewpager (added edit above) – DIRTY DAVE May 26 '19 at 20:46
  • I see. Have you seen the answers in [this SO question on how to retrieve Fragments from a ViewPager](https://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager/15261142#15261142)? – PPartisan May 26 '19 at 20:50
  • everything is so hacky I was just looking for a simple method to add "A_TAG" like you mentioned above lol – DIRTY DAVE May 26 '19 at 20:55

1 Answers1

1

Answer & Credits to the person who found this method:

How to get existing fragments when using FragmentPagerAdapter

I got the tag name that is automatically generated by android api

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// get the tags set by FragmentPagerAdapter
switch (position) {
    case 0:
        String firstTag = createdFragment.getTag();
        break;
    case 1:
        String secondTag = createdFragment.getTag();
        break;
}
// ... save the tags somewhere so you can reference them later
    return createdFragment;
}

Make sure you use the getChildFragmentManager to reference it otherwise it returns null I spent like 2 hours trying to find out why findFragmentByTag was returning null

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • https://stackoverflow.com/questions/54279509/how-to-get-elements-of-fragments-created-by-viewpager-in-mainactivity/54280113#54280113 <-- this approach would also work – EpicPandaForce May 28 '19 at 22:41