Skip to content Skip to sidebar Skip to footer

Call Method From Fragment To Activity

hello i have activity and i call many fragment based on my application business i need to call method from fragment in activity i searched in the internet but i cannot find the sol

Solution 1:

Just call the method of Fragment by creating the object of it.

HomeFragmenthomeFragment=newHomeFragment();
homeFragment.addUserLineInfoFragment();

Solution 2:

Call method of Activity from fragment

 ((YourActivityName)getActivity()).addUserLineInfoFragment();

Call method of fragment from Activity

1. Create Interface

publicinterfaceOnButtonListener
    {
        voidonButtonClick();
    }

2. Init in Activity

protectedOnButtonListener onActionButtonListener;

    publicvoidsetOnButtonListener(OnButtonListener actionButtonListener)
    {
        this.onActionButtonListener = actionButtonListener;
    }

3. In Activity, click event when this action need to perform

this.onActionButtonListener.onButtonClick();

4. Implement listener(OnButtonListener) in Fragment

@OverridepublicvoidonButtonClick(){}

5. In fragment onCreateView

((YourActivityName) getActivity()).setOnButtonListener(this);

Solution 3:

You can Broadcast Intent from the Fragment to activity after you get the bundle

@OverrideprotectedvoidonPostExecute(Object o) {
    super.onPostExecute(o);
    Intentintent=newIntent("key_to_identify_the_broadcast");
    Bundlebundle=newBundle();
    bundle.putString("edttext", json.toString());
    intent.putExtra("bundle_key_for_intent", bundle);
    context.sendBroadcast(intent);
}

and then you can receive the bundle in your Activity by using the BroadcastReceiver class

privatefinalBroadcastReceivermHandleMessageReceiver=newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        Bundlebundle= 
            intent.getExtras().getBundle("bundle_key_for_intent");
            if(bundle!=null){
                Stringedttext= bundle.getString("edttext");
            }
            //you can call any of your methods for using this bundle for your use case
    }
};

in onCreate() of your Activity you need to register the broadcast receiver first otherwise this broadcast receiver will not be triggered

IntentFilter filter=new IntentFilter("key_to_identify_the_broadcast");
getActivity().getApplicationContext().
               registerReceiver(mHandleMessageReceiver, filter);

Finally you can unregister the receiver to avoid any exceptions

@Override
public void onDestroy() {
    try {

         getActivity().getApplicationContext().
             unregisterReceiver(mHandleMessageReceiver);

    } catch (Exception e) {
        Log.e("UnRegister Error", "> " + e.getMessage());
    }
    super.onDestroy();
}

You can create separate broadcast receivers in all of your fragments and use the same broadcast to broadcast the data to your Activity. You can also use different keys for different fragments and then broadcast using particular key for particular fragment.

Solution 4:

Your solution is pretty simple, find the fragment instance assuming you have already added it, and call your method as follow:

HomeFragmenthomeFragment= (HomeFragment)getSupportFragmentManager().findFragmentByTag("myFragmentTag");
if(homeFragment != null) //check for null
    homeFragment.addUserLineInfoFragment();

Post a Comment for "Call Method From Fragment To Activity"