0

I have an activity which manages four fragments via a ViewPagerAdapter. From my activity, I want to call a method:

public void openButtons(){
//mPosition is a position of pager

    if (mPosition==0){
        Fragment1 fragment = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
        fragment.openButtons();


    }
    if (mPosition==1){
        Fragment2 fragment = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        fragment.openButtons();
    }
    if (mPosition==2){
        ....
    }
    if (mPosition==3){
        ...
    }

}

If the method in my fragment is defined as non-static:

public void openButtons(){//some stuff}

I get a nullpointer for fragment.openButtons () line and this whatever the position and the fragment.

If the method is declared as static, it's ok.

public static void openButtons(){//some stuff}

The content of the method is not in question because the problem is the same with an empty method.

So my question is why we have to define the static method in the fragment?

'Cause in these conditions:

public void openButtons(){
//mPosition is a position of pager

    if (mPosition==0){
        Fragment1.openButtons()


    }
    if (mPosition==1){
         Fragment2.openButtons()
    }
    if (mPosition==2){
        ....
    }
    if (mPosition==3){
        ...
    }

}

is equally functional!

Thanks.

Aristide13
  • 244
  • 2
  • 16

4 Answers4

1

Casting null to a reference won't throw an exception, to a primitive, it will.

Use findFragmentById() or findFragmentByTag() to get a reference and check if its null, if not, check the reference's isAdded() or isVisible().

PlayerFragment p = (PlayerFragment) mManager.findFragmentById(R.id.bottom_container);
if( p != null){
  if(p.isAdded()){
    p.onNotificationListener.updateUI();
  }
}
Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
  • I do not understand your explanation of the link with the fact that the method should be called static ?? – Aristide13 Jul 06 '15 at 09:26
  • 1
    it's not necessary method is static.you have to call fragment method so you have to check that fragment is not null and fragment is added and active after that you have to called method of fragment.So in above code i have to check all this condition – Darshan Mistry Jul 06 '15 at 09:34
  • That is precisely what I do not understand: the NullPointer execption is targeted to the method in case it is not static is seen as null! So why I do not have nullpointer for: Fragment1 fragment = (Fragment1) getSupportFragmentManager () findFragmentById (R.id.fragment1). When the method is static, and vice versa when the method is not static! Also in your code where does the function: onNotificationListener? Thank you very much for your help ... – Aristide13 Jul 06 '15 at 09:52
  • In my case the fragment is necessarily active since managed by a viewPager. The fragment is visible when I call the method from the parent activity – Aristide13 Jul 06 '15 at 09:59
1

therefore, in the case of viewPager, find the instance of the fragment by its id or tag, is not the right approach.

It is better to do the following:

public void openButtons() {
    // mPosition is a position of pager

    ViewPagerAdapter adapter = ((ViewPagerAdapter) mViewPager.getAdapter());

    if (mPosition == 0) {
        Fragment fragment = adapter.getItem(0);
        ((Fragment1)fragment).openButtons();
    }

    if (mPosition == 1){
        Fragment fragment = adapter.getItem(1);
        ((Fragment2)fragment).openButtons();
    }

    if (mPosition == 2){
        ....
    }

    if (mPosition == 3){
        ...
    }
}

Merci.

Eloims
  • 5,106
  • 4
  • 25
  • 41
Aristide13
  • 244
  • 2
  • 16
1
//From fragment to activty:
((YourActivityClassName)getActivity()).yourPublicMethod();



//From activity to fragment:
FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = 
(YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();

//If you added fragment via code and used a **tag** string when you added your 
fragment, use **findFragmentByTag** instead:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
Bharat Vasoya
  • 351
  • 4
  • 7
0

You have to get instance of your fragment then call your public method:

In your fragment :

 private static yourFragment instance;

then in onCreateView of your fragment :

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {

        instance= this;

        View v = inflater.inflate(R.layout.fragment_tools, container, false);
        binding = FragmentToolsBinding.inflate(inflater, container, false);

        return v;
    }

and also in your fragment you have to have a static method that returns the instance:

public static yourFragment GetInstance()
{
    return instance;
}

then you have a public method in in your fragment that you want to call it like this:

public  void  openButtons()
{
    Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
}

then you can get fragment instance and call your non static public method like this:

public void openButtons() {
    // mPosition is a position of pager

    ViewPagerAdapter adapter = ((ViewPagerAdapter) mViewPager.getAdapter());

    if (mPosition == 0) {
        yourFragment frag = yourFragment.GetInstance();
            frag.openButtons();
    }
}
da jowkar
  • 181
  • 1
  • 8