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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM