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