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

  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4816071.html
Copyright © 2011-2022 走看看