I know there are questions like:
android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.
In my Activity 1
:
public void goToMapView(Info info) {
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
}
In Info:
public void write(Intent intent) {
Bundle b = new Bundle();
b.putInt(AppConstants.ID_KEY, id);
... //many other attributes
intent.putExtra(AppConstants.BUNDLE_NAME, b);
}
public static Info read(Bundle bundle) {
Info info = new Info();
info.setId(bundle.getInt(AppConstants.ID_KEY));
... //many other attributes
return info;
}
In MapViewActivity (Activity 2
):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
info = Info.read(extras);
...
}
Problem is that extras
bundle is always null. I have debugged it and Intent (intent = getIntent()
) has all fields set to null except one indicating what class this is (MapViewActivity
).
I've also tried putting the bundle via intent.putExtras(b)
with the same effect.
The intent.putExtra("asdf", true)
is for debugging reasons only - I cannot get this data too (because getIntent()
has almost all fields set to null).
EDIT
The answers below are correct and working. This was my fault. I didn't correctly passed my bundle to new intent.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…