【问题标题】:How to send information of fragment to activity and In reverse in android [duplicate]如何将片段信息发送到活动并在android中反向[重复]
【发布时间】:2017-10-09 11:02:57
【问题描述】:

如何将活动信息发送到片段以及将片段信息发送到活动 在安卓中?

【问题讨论】:

    标签: android android-fragments android-fragmentactivity


    【解决方案1】:

    您的问题非常不好并且不清楚!提问前请先阅读this link再提问。

    但要在Activity 中使用来自Fragment 的自定义方法,您可以使用我下面的示例。

    首先在你的活动中写下你的方法:

    public void showMessage(){
        Toast.makeText(context, "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
    }
    

    然后,您应该在 Fragment 中创建活动实例:

    public class YOUR_FRAGMENT_NAME extends Fragment {
       private YOUR_ACTIIVTY_NAME yourActivity;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_layout, container, false);
    
            //Initialize
            mainActivity = (MainActivity) getActivity();
    
            //Call method 
            yourActivity.showMessage();
    
        return view;
    }
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢工程师的最佳答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多