bundle堆栈与每个startActivity()?



我有3个活动,A, B和c。如果我将一个意图从A传递到B

//from A
Intent i1 = new Intent(A.this, B.class);
Bundle firstBundle = new Bundle();
firstBundle.putString("Key1", "Value1");
i1.putExtras(firstBundle);
//to B
Intent i1 = getIntent();

如果我从B添加另一个bundle到C

//from B
Intent i2 = new Intent(B.this, C.class);
Bundle secondBundle = new Bundle();
secondBundle.putString("Key2", "Value2");
i2.putExtras(secondBundle);
//to C
Intent i2 = getIntent();

我能在C中使用这个吗?

Intent i3 = getIntent();
Bundle thirdBundle = i3.getExtras();
String firstString = thirdBundle.getString("firstKey");

我可以从firstBundle获得物品,即使它来自不同的意图?如果不是,我该如何让它发生?这样做有效率吗?有没有更好的办法?

要从现有意图中获取数据,您需要调用返回当前BundlegetIntent().getExtras()。您正在用新实例创建secondBundle,但您需要使用Bundle secondBundle = getIntent().getExtras();

最新更新