简体   繁体   English

从片段到活动的调用方法

[英]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");
    }

Just call the method of Fragment by creating the object of it. 只需通过创建Fragment的对象来调用它的方法。

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

You can Broadcast Intent from the Fragment to activity after you get the bundle 获得捆绑包后,您可以将Intent从Fragment广播到活动

@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 然后您可以使用BroadcastReceiver类在活动中接收捆绑

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 在Activity的onCreate()中,您需要先注册广播接收器,否则将不会触发此广播接收器

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. 您还可以对不同的片段使用不同的密钥,然后对特定的片段使用特定的密钥进行广播。

Call method of Activity from fragment 从片段调用Activity的方法

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

Call method of fragment from Activity 从Activity调用片段的方法

1. Create Interface 1.创建界面

 public interface OnButtonListener
    {
        void onButtonClick();
    }

2. Init in Activity 2.初始化活动

 protected OnButtonListener onActionButtonListener;

    public void setOnButtonListener(OnButtonListener actionButtonListener)
    {
        this.onActionButtonListener = actionButtonListener;
    }

3. In Activity, click event when this action need to perform 3.在活动中,在需要执行此操作时单击事件

this.onActionButtonListener.onButtonClick();

4. Implement listener(OnButtonListener) in Fragment 4.在片段中实现侦听器(OnButtonListener)

 @Override
    public void onButtonClick(){}

5. In fragment onCreateView 5.在片段onCreateView

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM