Moayed Alayaseh
Moayed Alayaseh

Reputation: 243

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 solution

this is my fragment :

public class HomeFragment extends Fragment implements View.OnClickListener {
public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d("onCreate", "onCreateViewHF");
        view = inflater.inflate(R.layout.new_fragment_home, container, false);
}

/// this method i need to call in Activity 
 public void addUserLineInfoFragment() {
        userLineInfoFragment = new UserLineInfoFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.user_info_fragment_container, userLineInfoFragment).commit();
        Log.d("HOMMETEST","HOMMMME");
    }

Upvotes: 0

Views: 10645

Answers (4)

Manoj Bhadane
Manoj Bhadane

Reputation: 617

Call method of Activity from fragment

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

Call method of fragment from Activity

1. Create Interface

 public interface OnButtonListener
    {
        void onButtonClick();
    }

2. Init in Activity

 protected OnButtonListener onActionButtonListener;

    public void setOnButtonListener(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

 @Override
    public void onButtonClick(){}

5. In fragment onCreateView

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

Upvotes: 2

Ricardo
Ricardo

Reputation: 9656

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

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

Upvotes: 0

Vicky Kumar
Vicky Kumar

Reputation: 154

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

HomeFragment homeFragment = new HomeFragment();
homeFragment.addUserLineInfoFragment();

Upvotes: 2

phpdroid
phpdroid

Reputation: 1663

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

@Override
protected void onPostExecute(Object o) {
    super.onPostExecute(o);
    Intent intent = new Intent("key_to_identify_the_broadcast");
    Bundle bundle = new Bundle();
    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

private final BroadcastReceiver mHandleMessageReceiver = new 
BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = 
            intent.getExtras().getBundle("bundle_key_for_intent");
            if(bundle!=null){
                String edttext = 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.

Upvotes: 0

Related Questions