[英]Data not passing from activity to fragment
該程序有一個DisplayActivity
類,該類從LoginActivity
類接收數據。 DisplayActivity
有一個Fragment
來顯示從LoginActivity
傳遞的數據。 問題在於DisplayFragment
類收到的Bundle
為null。因此數據未顯示在Activity
。 我正在附上代碼。 請突出顯示錯誤。
DisplayActivity
類
public class DisplayActivity extends AppCompatActivity {
private Toolbar toolbar;
private Button editButton;
private static Bundle infoBundle;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_display);
infoBundle = getIntent().getExtras();
DisplayFragment details = new DisplayFragment();
details.setArguments(infoBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_details, details).commit();
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("DisplayScreen");
editButton = (Button) findViewById(R.id.edit_button);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent backIntent = new Intent(DisplayActivity.this, LoginActivity.class);
backIntent.putExtras(infoBundle);
startActivity(backIntent);
finish();
}
});
}
DisplayFragment
類
public class DisplayFragment extends Fragment {
public static Bundle backBundle;
private TextView mTextView;
public DisplayFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
backBundle = getArguments();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_details, container, false);
if(backBundle != null) {
String username = backBundle.getString("UserName");
String emailID = backBundle.getString("E-mail");
String gender = backBundle.getString("Gender");
String city = backBundle.getString("City");
mTextView = inflatedView.findViewById(R.id.user_text);
mTextView.setText(username);
mTextView.append("\n");
mTextView.append(emailID + "\n");
mTextView.append(gender);
if (!city.equals("---Select---"))
mTextView.append("\n" + city);
}
return inflatedView;
}
}
我給出了LoginActivity,DisplayActivity和DisplayFragment的完整代碼,其中DisplayFragment接收通過DisplayActivity從LoginActivity傳遞的值。 嘗試這個
LoginActivity:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.btnSubmit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "My name is ABCD");
Intent i = new Intent(LoginActivity.this, DisplayActivity.class);
i.putExtras(bundle);
startActivity(i);
}
});
}
}
DisplayActivity:
public class DisplayActivity extends AppCompatActivity {
private Button editButton;
private Bundle infoBundle;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_display);
infoBundle = getIntent().getExtras();
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_container);
/*infoBundle = new Bundle();
infoBundle.putString("name", "ABCD");*/
DisplayFragment details = new DisplayFragment();
details.setArguments(infoBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, details).commit();
}
}
和DisplayFragment:
public class DisplayFragment extends Fragment {
public static Bundle backBundle;
private TextView mTextView, tvValue;
public DisplayFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
backBundle = getArguments();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_details, container, false);
if (backBundle != null) {
String username = "";
username = backBundle.getString("name");
tvValue = (TextView) inflatedView.findViewById(R.id.tvValue);
tvValue.setText(username);
}
return inflatedView;
}
@Override
public void onResume() {
super.onResume();
}
}
請在您的AndroidManifest.xml中為LoginActivity和DisplayActivity添加權限
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.