简体   繁体   中英

how can I pass parameter from Activity to function in Fragment

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, ...);

For passing messages between various components of your app, I would highly recommend you to use the seasoned solution of publisher/subscriber using EventBus

  • To add EventBus as a dependency in your project, add the following line in your app-level build.gralde file:
    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.

  • Define your event class as a simple POJO:
    public class MessageEvent {
        public final String message;

        public MessageEvent(String message) {
            this.message = message;
        }
    }
  • In your Fragment, add this code to listen to the event
    // 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
    }
  • In your Fragment, add this code to register to and unregister from the bus:
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }
  • Finally, from your Activity, post events:
    EventBus.getDefault().post(new MessageEvent("Hello everyone!"));

Your Fragment will receive this message.


Coming to your particular example, you can do so as follows:

  • Your event POJO class should be:
    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
        }
    }
  • When the event occurs, you can execute your desired method in your Fragment as:
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        getViewProfileMainActivity(event.SendViewProfileName, ...);
    }

    private getViewProfileMainActivity(Profile, ...) {
         // your function definition here
    }
  • From your Activity, you can post the event as:
    EventBus.getDefault().post(new MessageEvent(SendViewProfileName, SendViewProfileEmail, ...));

Hope this helps!

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

I don't have enough reputation to put a comment.

You have a similar answer here

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM