从片段到Activity的调用方法

时间:2017-09-11 08:08:12

标签: android android-fragments

你好,我有活动,我根据我的应用程序业务调用许多片段我需要从互联网上搜索的活动中的片段调用方法,但我找不到解决方案

这是我的片段:

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

4 个答案:

答案 0 :(得分:1)

只需通过创建它的对象来调用Fragment的方法。

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

答案 1 :(得分:0)

您可以在获得捆绑包后将片段中的意图广播到活动

@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);
}

然后您可以使用BroadcastReceiver类

在Activity中接收该包
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
    }
};

在您的活动的onCreate()中,您需要先注册广播接收器,否则不会触发此广播接收器

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

最后,您可以取消注册接收器以避免任何异常

@Override
public void onDestroy() {
    try {

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

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

您可以在所有片段中创建单独的广播接收器,并使用相同的广播将数据广播到您的活动。您还可以对不同的片段使用不同的键,然后使用特定片段的特定键进行广播。

答案 2 :(得分:0)

从片段

调用Activity的方法
 ((YourActivityName)getActivity()).addUserLineInfoFragment();

从活动

调用片段的方法

<强> 1。创建界面

 public interface OnButtonListener
    {
        void onButtonClick();
    }

<强> 2。在活动中启动

 protected OnButtonListener onActionButtonListener;

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

第3。在“活动”中,单击此操作需要执行的事件

this.onActionButtonListener.onButtonClick();

<强> 4。在Fragment

中实现监听器(OnButtonListener)
 @Override
    public void onButtonClick(){}

<强> 5。在片段onCreateView

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

答案 3 :(得分:0)

您的解决方案非常简单,假设您已经添加了片段实例,并按照以下方式调用您的方法:

m2
相关问题