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共享

  • 相关阅读:
    [OS] 信号量(Semaphore)
    [OS] 进程互斥
    [剑指Offer] 52.正则表达式匹配
    [剑指Offer] 51.构建乘积数组
    [剑指Offer] 50.数组中重复的数字
    [剑指Offer] 49.把字符串转换成整数
    [剑指Offer] 48.不用加减乘除做加法
    [剑指Offer] 47.求1+2+3+...+n
    PHP知识库图谱汇总(完善中)
    修改thinkphp路由模式,去掉Home
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4816071.html
Copyright © 2011-2022 走看看