zoukankan      html  css  js  c++  java
  • Android Activity和Fragment传递数据

    1、Activity与Activity传递数据

    UserLoginActivity.java:

    Intent welcomePage = new Intent();
    Bundle dataBundle = new Bundle();//将数据放在Bundle中
    dataBundle.putString("email", mEmail);
    dataBundle.putString("password", mPassword);
    welcomePage.putExtras(dataBundle);//讲数据放入下一个Intent
    welcomePage.setClass(UserLoginActivity.this, WelcomeActivity.class);
    startActivity(welcomePage);

    WelcomeActivity.java:

    Bundle dataBundle = this.getIntent().getExtras();//获得当前Intent内数据Bundle
    String email = dataBundle.getString("email");//从Bundle中获得对应数据
    TextView showEmail = (TextView)findViewById(R.id.showEmail);//查找Activity中的View
    showEmail.setText("欢迎您~:"+email);

    2、Activity与Fragment 传值

    UserLoginActivity.java:同上

    WelcomeActivity.java中Fragment,在onCreateView方法内:

    View rootView = inflater.inflate(R.layout.fragment_welcome,container, false);//获得根视图
    Bundle dataBundle = getActivity().getIntent().getExtras();//从当前<span style="font-family: Arial, Helvetica, sans-serif;">Activity中获得Intent,并获得数据Bundle</span>
    String email = dataBundle.getString("email");
    TextView showEmail = (TextView)rootView.findViewById(R.id.showEmail_fragment);//从根视图中查找View
    showEmail.setText("Fragment欢迎您~:"+email);

    3、Activity获得Fragment :

    getFragmentManager().findFragmentById(R.layout.fragment_main);

    其他更多Activity、Fragment  交互 和 通信 待调研~

    Fragment限制:不能跨Activity共享

  • 相关阅读:
    Thinkphp6.0/TP6框架中新增函数的解释及用法
    PHP7.1的几个新特性
    tp6数据库mysql调试
    nginx基本配置
    redis配置
    redis的笔记
    thinkphp学习笔记
    vsCode软件相关快捷键
    erlang随笔3--OTP
    文献综述二十:基于UML技术的客户关系管理系统实现
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4816071.html
Copyright © 2011-2022 走看看