【问题标题】:how can I pass parameter from Activity to function in Fragment如何将参数从 Activity 传递到 Fragment 中的函数
【发布时间】:2019-10-18 08:18:38
【问题描述】:

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

Activity.java

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

Fragment.java

getViewProfileMainActivity(String Profile, ...);

【问题讨论】:

    标签: java android android-activity fragment


    【解决方案1】:

    为了在应用程序的各个组件之间传递消息,我强烈建议您使用经验丰富的发布者/订阅者解决方案,使用 EventBus

    • 要将 EventBus 作为依赖项添加到您的项目中,请在您的应用级 build.gralde 文件中添加以下行:
        implementation 'org.greenrobot:eventbus:3.1.1'
    

    请注意,在撰写此答案时,最新版本是 3.1.1。您应该检查here 的最新版本并将其包含在内。

    • 将您的事件类定义为一个简单的 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
        }
    
    • 在您的 Fragment 中,添加此代码以注册和注销总线:
        @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!"));
    

    您的 Fragment 将收到此消息。


    来到你的具体例子,你可以这样做:

    • 您的事件 POJO 类应该是:
        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
        }
    
    • 在您的活动中,您可以将活动发布为:
        EventBus.getDefault().post(new MessageEvent(SendViewProfileName, SendViewProfileEmail, ...));
    

    希望这会有所帮助!

    【讨论】:

    • 哇,它的工作原理,谢谢!,这个库是不是有什么不好的影响,比如低于 21 的 API 会崩溃?
    • 不客气!我已经在各种生产级应用程序中使用这个库超过 4 年了,下载量超过 1000 万次,完全没有遇到任何问题。
    • 多么宽慰,因为我需要将这个应用程序发布到任何类型的操作系统,谢谢这个库也帮助我避免在片段之间切换时崩溃
    【解决方案2】:

    https://developer.android.com/training/basics/fragments/communicating.html#Deliver接受培训

    您只需要在 Activity 中获取片段

    ArticleFragment articleFrag = (ArticleFragment)
                    getSupportFragmentManager().findFragmentById(R.id.article_fragment);
    
    

    如果还不存在则处理

    【讨论】:

    • 它有效,但是当我在片段之间切换时它会崩溃,进程:com.indofun.android.indojoy,PID:813 java.lang.ClassCastException:com.indofun.android.indojoy.Support。 HomePageFragment 无法在 com.indofun.android.indojoy.MainActivity.SetViewProfile(MainActivity.java:1579) 在 com.indofun.android.indojoy.MainActivity$18$1.run( MainActivity.java:1423)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多