call method from fragment to Activity

前端 未结 4 2010
孤城傲影
孤城傲影 2021-01-26 06:53

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

相关标签:
4条回答
  • 2021-01-26 07:24

    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.

    0 讨论(0)
  • 2021-01-26 07:42

    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);
    
    0 讨论(0)
  • 2021-01-26 07:46

    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();
    
    0 讨论(0)
  • 2021-01-26 07:48

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

    HomeFragment homeFragment = new HomeFragment();
    homeFragment.addUserLineInfoFragment();
    
    0 讨论(0)
提交回复
热议问题