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