[英]Passing Data from ACTIVITY to FRAGMENT not working with me
活動 :
boolean isExist = Mydb.checkUserExist(editusername.getText().toString(), editpassword.getText().toString());
if(isExist==true){
Intent intent = new Intent (LoginActivity.this,DressyActivity.class);
//PASSING DATA TO protfilefragment
Bundle bundle=new Bundle();
bundle.putString("uesername",editusername.getText().toString());
protfileFragment f=new protfileFragment();
f.setArguments(bundle);
//
startActivity(intent); }
片段:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_profile ,container,false);
//RECIVE DATA
welcome=(TextView)view.findViewById(R.id.textView2);
if(this.getArguments()!=null){
String passed_data=this.getArguments().getString("username");
welcome.setText(passed_data); }
//
return view;
}
問題是您正在啟動 DressyActivity 並將數據傳遞給片段,
由於 profileFragment 由它的父 Activity 處理,因此您應該首先將數據傳遞給它的父 Activity。 這將是數據流。
LoginActivity → DressyActivity → profileFragment
所以在 LoginActivity
Intent intent = new Intent(LoginActivity.this,DressyActivity.class);
intent.putString("key","value");
startActivity(intent);
然后在 onCreate() 方法上的 DressyActivity 中將數據傳遞給 profileFragment
// Receive Data From LoginActivity
String value = getIntent().getStringExtra("key");
// Then passs that data to Fragment
Bundle bundle = new Bundle();
bundle.putString("key","value");
profileFragment profilefrag = new profileFragment();
profilefrag.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment_container,profilefrag,"frag_profile")
.commit();
然后就可以在 profileFragment 中接收數據了
if(this.getArguments()!=null){
String passed_data=this.getArguments().getString("key");
welcome.setText(passed_data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.