Search code examples
androidandroid-fragmentsandroid-activityandroid-viewpagerswipe

Call a Fragment's method from parent Activity using a ViewPager


I have some sample code from Slidenerd's Scrolltab video series and this works perfectly well. However, I cannot call a method inside a fragment from the parent activity. For example, I have included a method inside fragment A called doSomething(). I'd like to call it from MainActivity's onResume() callback.

Edit: Using the "accepted answer" works for this application but not for the actual application that I'm working on due to a nullpointerexception.

Error log

06-25 13:02:19.308    3862-3862/com.felhr.agweigh D/AndroidRuntime﹕ Shutting down VM
06-25 13:02:19.308    3862-3862/com.felhr.agweigh W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416ecda0)
06-25 13:02:19.308    3862-3862/com.felhr.agweigh E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.felhr.agweigh, PID: 3862
    java.lang.RuntimeException: Error receiving broadcast Intent { act=com.felhr.usbservice.USB_PERMISSION_GRANTED flg=0x10 } in com.felhr.agweigh.MainActivity$3@41da2c10
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5479)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.felhr.agweigh.FragmentPlotCSV.createSpinner(FragmentPlotCSV.java:102)
            at com.felhr.agweigh.MainActivity$3.onReceive(MainActivity.java:894)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5479)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.felhr.agweigh" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.usb.host"
        android:required="true" />

    <application
        android:logo="@drawable/rinstrumlogo"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/RinstrumActionBarTheme" >
        <activity
            android:name="com.felhr.agweigh.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.felhr.agweigh.UsbService"
            android:enabled="true" >
        </service>

    </application>

</manifest>

MainActivity

public class MainActivity extends FragmentActivity {

ViewPager viewPager = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager)findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));
}

@Override
protected void onResume() {
    super.onResume();
}
}

MyAdapter

class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm)
{
    super(fm);
}

@Override
public Fragment getItem(int i) {
    Fragment fragment = null;
    Log.d("SWIPE","get Item is called"+i);
    if (i==0)
    {
        fragment=new FragmentA();
    }
    if (i==1)
    {
        fragment=new FragmentB();
    }
    if (i==2)
    {
        fragment=new FragmentC();
    }
    return fragment;
}

@Override
public int getCount() {
    Log.d("SWIPE","get count is called");
    return 3;
}

// talks to title to give the page
@Override
public CharSequence getPageTitle(int position) {
    if (position == 0) {
        return "Tab 1";
    }
    if (position == 1) {
        return "Tab 2";
    }
    if (position == 2) {
        return "Tab 3";
    }
    return null;
}
}

FragmentA

/**
 * Created by Luke on 20/04/2015.
 */
public class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    public void doSomething(){
        // call this method from main activity
        }
    }

FragmentB

/**
 * Created by Luke on 20/04/2015.
 */
public class FragmentB extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

FragmentC

/**
 * Created by Luke on 20/04/2015.
 */
public class FragmentC extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

Solution

  • You can do something like this-

        @Override
        protected void onResume() {
            super.onResume();
            FragmentA _fragA = new FragmentA();
            _fragA.doSomething();
    
        }
    

    You can register your receiver in the manifest file by using the element:

           <receiver android:name="MyReceiver" >
                <intent-filter>
                    <action android:name="com.example.Broadcast" >
                        </action>
                </intent-filter>
            </receiver>
    

    The nested element is used to specify the event the receiver should react to.