如何将 Activity 中的参数传递给 Fragment 中的函数
how can I pass parameter from Activity to function in Fragment
我想将我的参数从 activity 传递到片段,我该怎么做?
Activity.java
fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl);
Fragment.java
getViewProfileMainActivity(String Profile, ...);
为了在应用程序的各个组件之间传递消息,我强烈建议您使用 publisher/subscriber 的经验丰富的解决方案,使用 EventBus
- 要将 EventBus 添加为项目中的依赖项,请在应用级 build.gralde 文件中添加以下行:
implementation 'org.greenrobot:eventbus:3.1.1'
请注意,在撰写此答案时,最新版本为 3.1.1。您应该检查 here 的最新版本并将其包括在内。
- 将事件 class 定义为简单的 POJO:
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
- 在你的 Fragment 中,添加这段代码来监听事件
// This method will be called when a MessageEvent is posted (in the UI thread for Toast)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
// do something here
}
- 在您的片段中,添加此代码以注册和注销总线:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
- 最后,根据您的 Activity、post 事件:
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
您的片段将收到此消息。
对于您的特定示例,您可以按如下方式进行操作:
- 您的事件 POJO class 应该是:
public class MessageEvent {
public final String SendViewProfileName;
public final String SendViewProfileEmail;
// similarly other params
public MessageEvent(String SendViewProfileName, String SendViewProfileEmail, ...) {
this.SendViewProfileName = SendViewProfileName;
this.SendViewProfileEmail = SendViewProfileEmail;
// similarly other params
}
}
- 当事件发生时,您可以在您的 Fragment 中执行您想要的方法:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
getViewProfileMainActivity(event.SendViewProfileName, ...);
}
private getViewProfileMainActivity(Profile, ...) {
// your function definition here
}
- 从您的 Activity,您可以 post 事件作为:
EventBus.getDefault().post(new MessageEvent(SendViewProfileName, SendViewProfileEmail, ...));
希望对您有所帮助!
在 https://developer.android.com/training/basics/fragments/communicating.html#Deliver
的培训中涵盖
您只需要在 Activity
中获取片段
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
不存在则处理
我想将我的参数从 activity 传递到片段,我该怎么做?
Activity.java
fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl);
Fragment.java
getViewProfileMainActivity(String Profile, ...);
为了在应用程序的各个组件之间传递消息,我强烈建议您使用 publisher/subscriber 的经验丰富的解决方案,使用 EventBus
- 要将 EventBus 添加为项目中的依赖项,请在应用级 build.gralde 文件中添加以下行:
implementation 'org.greenrobot:eventbus:3.1.1'
请注意,在撰写此答案时,最新版本为 3.1.1。您应该检查 here 的最新版本并将其包括在内。
- 将事件 class 定义为简单的 POJO:
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
- 在你的 Fragment 中,添加这段代码来监听事件
// This method will be called when a MessageEvent is posted (in the UI thread for Toast)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
// do something here
}
- 在您的片段中,添加此代码以注册和注销总线:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
- 最后,根据您的 Activity、post 事件:
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
您的片段将收到此消息。
对于您的特定示例,您可以按如下方式进行操作:
- 您的事件 POJO class 应该是:
public class MessageEvent {
public final String SendViewProfileName;
public final String SendViewProfileEmail;
// similarly other params
public MessageEvent(String SendViewProfileName, String SendViewProfileEmail, ...) {
this.SendViewProfileName = SendViewProfileName;
this.SendViewProfileEmail = SendViewProfileEmail;
// similarly other params
}
}
- 当事件发生时,您可以在您的 Fragment 中执行您想要的方法:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
getViewProfileMainActivity(event.SendViewProfileName, ...);
}
private getViewProfileMainActivity(Profile, ...) {
// your function definition here
}
- 从您的 Activity,您可以 post 事件作为:
EventBus.getDefault().post(new MessageEvent(SendViewProfileName, SendViewProfileEmail, ...));
希望对您有所帮助!
在 https://developer.android.com/training/basics/fragments/communicating.html#Deliver
的培训中涵盖您只需要在 Activity
中获取片段ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
不存在则处理