Reputation: 191
I want to pass my parameter from activity to fragment how can I do it?
Activity.java
fragment.getViewProfileMainActivity(SendViewProfileName, SendViewProfileEmail, SendViewProfilePhone, SendViewProfileCity, SendViewProfileGender, SendViewProfileBirthdate, SendViewProfilePhotoUrl);
Fragment.java
getViewProfileMainActivity(String Profile, ...);
Upvotes: 0
Views: 94
Reputation: 10162
Covered in the training at https://developer.android.com/training/basics/fragments/communicating.html#Deliver
You just need get the fragment in your Activity
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
and then handle if it does not exist yet
Upvotes: 1
Reputation: 1700
For passing messages between various components of your app, I would highly recommend you to use the seasoned solution of publisher/subscriber using EventBus
implementation 'org.greenrobot:eventbus:3.1.1'
Note that at the time of writing this answer, the latest version was 3.1.1. You should check the latest version from here and include that.
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
// 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();
}
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
Your Fragment will receive this message.
Coming to your particular example, you can do so as follows:
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
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
getViewProfileMainActivity(event.SendViewProfileName, ...);
}
private getViewProfileMainActivity(Profile, ...) {
// your function definition here
}
EventBus.getDefault().post(new MessageEvent(SendViewProfileName, SendViewProfileEmail, ...));
Hope this helps!
Upvotes: 1