Call a non static method in fragment from activity?

call a method in main activity
how to call non static method from static method in android
call activity method from application class
how to call activity method from service in android
how to call activity method from broadcastreceiver in android
android call function from another activity without static
call activity method from another activity android
how to call a class in android activity

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.

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();
  }
}

How to call a non-static method from one Activity to another activity , class A extends Activity{ static A instance; @Override protected void onCreate(​Bundle savedInstanceState) { super.onCreate(savedInstanceState); instance  Call an activity method from a fragment. Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity.

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.

Call an activity method from a fragment, From fragment to activty: ((YourActivityClassName)getActivity()).​yourPublicMethod();. From activity to fragment: FragmentManager fm  I want to call an activity method when a certain element is pressed on the fragment, however I can't get it to work. This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.

//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");

Calling a non-static fragment method from activity : learnandroid, Calling a non-static fragment method from activity. I'm trying to apply a SwipeRefreshLayout to a TabLayout. The main activity holds the SwipeRefreshLayout  Android call fragment method from activity. Sometimes, you have to call a method of a fragment from its parent activity. For example, you have a fragment that displays a list of SMS. When there is a new incoming SMS, the SMS receiver in your activity will call a method of the fragment to upload the SMS list.

Call non-static method (from subclass) in Java from Unity, In this class (PrefsFragment) I want to call a non-static method from Unity to extends Activity {; public static PrefsFragment mPrefsFragment;  OR a reference to non static method of object of a specific type. If onArticleSelected was a static method of MainActivity, we could refer to it via MainActivity::onArticleSelected . Referencing a static method is the most typical use of this type of reference. But, in our case,

How to call Main Activity method in another class in Android, It could be that the answer involves passing a reference to your Activity to another class, or using an interface , or This works for UI classes like Fragments and DialogFragments. Your method will not have access to other non static stuff . I see in the Android Fragments Dev Guide that an “activity can call methods in a fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag().”. The example that follows shows how to get a fragment reference, but not how to call specific methods in the fragment.

1 Error needs fix, I have implemented the fragment code into the main activity GuitarActivity. MyFragment1 is a static class, and I can't reference the non-static method from there. Since you don't have an instance, you can't make that call. 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 "); /* * Better approach: * Define an interface in the fragment, implement it in the activity * Call it from the

Comments
  • why not you made a static object reference of particular fragment? and with that obj you manage to call any method in fragment. no need to define a static method.
  • See this link might help you.
  • does getSupportFragmentManager().findFragmentById(R.id.fragment1); return null ?
  • Get fragment object from FragmentManager and then call the non-static methods.
  • Maybe casting can help you: ((Fragment1) fragment).openButtons(); This works in my case
  • I do not understand your explanation of the link with the fact that the method should be called static ??
  • 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
  • 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 ...
  • 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