[英]call method from fragment to Activity
你好,我有活动,我根据应用程序业务调用了很多片段,我需要从活动中的片段中调用方法,我在互联网上进行了搜索,但找不到解决方案
这是我的片段:
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");
}
只需通过创建Fragment的对象来调用它的方法。
HomeFragment homeFragment = new HomeFragment();
homeFragment.addUserLineInfoFragment();
获得捆绑包后,您可以将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);
}
然后您可以使用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
}
};
在Activity的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();
}
您可以在所有片段中创建单独的广播接收器,并使用相同的广播将数据广播到“活动”。 您还可以对不同的片段使用不同的密钥,然后对特定的片段使用特定的密钥进行广播。
从片段调用Activity的方法
((YourActivityName)getActivity()).addUserLineInfoFragment();
从Activity调用片段的方法
1.创建界面
public interface OnButtonListener
{
void onButtonClick();
}
2.初始化活动
protected OnButtonListener onActionButtonListener;
public void setOnButtonListener(OnButtonListener actionButtonListener)
{
this.onActionButtonListener = actionButtonListener;
}
3.在活动中,在需要执行此操作时单击事件
this.onActionButtonListener.onButtonClick();
4.在片段中实现侦听器(OnButtonListener)
@Override
public void onButtonClick(){}
5.在片段onCreateView
((YourActivityName) getActivity()).setOnButtonListener(this);
您的解决方案非常简单,假设您已经添加了片段实例,然后按如下所示调用您的方法:
HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().findFragmentByTag("myFragmentTag");
if(homeFragment != null) //check for null
homeFragment.addUserLineInfoFragment();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.