我正在尝试在完成Start_Activity后将一个madeObject从Start_Activity发送到Next_Activity。 如何在Start_Activity和Next_Activity中创建代码?
我的顺序是:
1)Start_Activity.onCreate()
2)Start_Activity.makeObjectData()
3)Start_Activity.putextras(madeObject)
3)Start_Activity.startActivity():启动Next_Activity。
4)Start_Activity.finish():完成Start_Activity
5)Next_Activity.getExtras()
这是Start_Activity
public class Start_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
:
//// makeDataObject ///
:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}
这是Next_Activity
public class Next_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
??? <-- How to get the madeDataObject of Start_Activity?
}
}
答案 0 :(得分:0)
bundle.putSerializable
有两个参数。第一个是String。它必须是此捆绑对象的唯一。之后您将使用它来检索您的对象。
bundle.putSerializable("my_unique_key", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
在Next_Activity
上,您可以使用getIntent()
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("my_unique_key");
}
当然,您尝试从一个活动传递到另一个活动的对象必须实现Serializable
答案 1 :(得分:-1)
将额外内容发送到下一个活动:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("myclass", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
在下一个活动中获取它:
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("myclass");
}