Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在片段中将参数从活动传递到函数_Java_Android_Android Activity_Fragment - Fatal编程技术网

Java 如何在片段中将参数从活动传递到函数

Java 如何在片段中将参数从活动传递到函数,java,android,android-activity,fragment,Java,Android,Android Activity,Fragment,我想将我的参数从活动传递到片段,我该怎么做 Activity.java fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl); getViewProfileMa

我想将我的参数从活动传递到片段,我该怎么做

Activity.java

fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl);
getViewProfileMainActivity(String Profile, ...);
Fragment.java

fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl);
getViewProfileMainActivity(String Profile, ...);

对于在应用程序的各个组件之间传递消息,我强烈建议您使用publisher/subscriber使用的经验丰富的解决方案

  • 要将EventBus添加为项目中的依赖项,请在应用程序级build.gralde文件中添加以下行:
实现'org.greenrobot:eventbus:3.1.1'
请注意,在撰写此答案时,最新版本为3.1.1。您应该从中检查最新版本,并包括该版本

  • 将事件类定义为简单的POJO:
公共类消息事件{
公共最终字符串消息;
公共消息事件(字符串消息){
this.message=消息;
}
}
  • 在片段中,添加以下代码以侦听事件
//在发布MessageEvent时(在Toast的UI线程中)将调用此方法
@订阅(threadMode=threadMode.MAIN)
MessageEvent(MessageEvent事件)上的公共无效{
Toast.makeText(getActivity(),event.message,Toast.LENGTH_SHORT).show();
//在这里做点什么
}
  • 在您的片段中,添加以下代码以向总线注册和从总线注销:
@覆盖
public void onStart(){
super.onStart();
EventBus.getDefault()寄存器(此);
}
@凌驾
公共void onStop(){
EventBus.getDefault().unregister(此);
super.onStop();
}
  • 最后,从您的活动中,发布事件:
EventBus.getDefault();
您的片段将收到此消息


关于您的特定示例,您可以按如下方式进行操作:

  • 您的事件POJO类应为:
公共类消息事件{
公共最终字符串SendViewProfileName;
公共最终字符串SendViewProfileEmail;
//类似地,其他参数
public MessageEvent(字符串SendViewProfileName,字符串SendViewProfileEmail,…){
this.SendViewProfileName=SendViewProfileName;
this.SendViewProfileEmail=SendViewProfileEmail;
//类似地,其他参数
}
}
  • 当事件发生时,您可以在片段中执行所需的方法,如下所示:
@Subscribe(threadMode=threadMode.MAIN)
MessageEvent(MessageEvent事件)上的公共无效{
getViewProfileMainActivity(event.SendViewProfileName,…);
}
私有GetViewProfileMain活动(配置文件,…){
//你的函数定义在这里
}
  • 通过您的活动,您可以将活动发布为:
EventBus.getDefault().post(新消息事件(SendViewProfileName,SendViewProfileEmail,…);

希望这有帮助

要在应用程序的各个组件之间传递消息,我强烈建议您使用publisher/subscriber的成熟解决方案,使用

  • 要将EventBus添加为项目中的依赖项,请在应用程序级build.gralde文件中添加以下行:
实现'org.greenrobot:eventbus:3.1.1'
请注意,在撰写此答案时,最新版本为3.1.1。您应该从中检查最新版本,并包括该版本

  • 将事件类定义为简单的POJO:
公共类消息事件{
公共最终字符串消息;
公共消息事件(字符串消息){
this.message=消息;
}
}
  • 在片段中,添加以下代码以侦听事件
//在发布MessageEvent时(在Toast的UI线程中)将调用此方法
@订阅(threadMode=threadMode.MAIN)
MessageEvent(MessageEvent事件)上的公共无效{
Toast.makeText(getActivity(),event.message,Toast.LENGTH_SHORT).show();
//在这里做点什么
}
  • 在您的片段中,添加以下代码以向总线注册和从总线注销:
@覆盖
public void onStart(){
super.onStart();
EventBus.getDefault()寄存器(此);
}
@凌驾
公共void onStop(){
EventBus.getDefault().unregister(此);
super.onStop();
}
  • 最后,从您的活动中,发布事件:
EventBus.getDefault();
您的片段将收到此消息


关于您的特定示例,您可以按如下方式进行操作:

  • 您的事件POJO类应为:
公共类消息事件{
公共最终字符串SendViewProfileName;
公共最终字符串SendViewProfileEmail;
//类似地,其他参数
public MessageEvent(字符串SendViewProfileName,字符串SendViewProfileEmail,…){
this.SendViewProfileName=SendViewProfileName;
this.SendViewProfileEmail=SendViewProfileEmail;
//类似地,其他参数
}
}
  • 当事件发生时,您可以在片段中执行所需的方法,如下所示:
@Subscribe(threadMode=threadMode.MAIN)
MessageEvent(MessageEvent事件)上的公共无效{
getViewProfileMainActivity(event.SendViewProfileName,…);
}
私有GetViewProfileMain活动(配置文件,…){
//你的函数定义在这里
}
  • 通过您的活动,您可以将活动发布为:
EventBus.getDefault().post(新消息事件(SendViewProfileName,SendViewProfileEmail,…);

希望这有帮助

你只需要在活动中获取片段

ArticleFr