我已经得到了一些自定义对象,如 RootObject
和表格
我要传递到下一个活动。
这是一个例子 RootObject
:
公共类RootObject
{
公共表格表格{获得;组; }
}
但我怎么能传递 RootObject
与下一个活动的意图
并获得在未来活动
?在表格
中,仍然有多个属性与列表
之类的东西,我需要访问一些属性在我的下活动
。我的意图被称为是这样的:
saveButton.Click + =委托{
如果(ValidateScreen()){
保存数据();
意向意图=新的意图(此的typeof(MainActivity));
叠B =新包();
b.PutSerializable(RootObject,RootObject);
StartActivity(意向);
}
};
解决方案
这是你如何去做。你的类需要实现Serializable或Parcelable。 在第一个活动(如你想从发送):
最终意向意图=新的意图(这一点,SecondActivity.class);
intent.putExtra(my_class,your_object);
startActivity(意向);
然后在你的第二个活动,你会怎么做:
最终意图passedIntent = getIntent();
最终YourClass my_class =(YourClass)passedIntent.getSerializableExtra(my_class);
也有这样做,使用捆绑::另一种方式
从你的类一样创建一个软件包:
公开捆绑toBundle(){
叠B =新包();
b.putString(SomeKey,someValue中);
返回b;
}
然后通过该软件包意图。现在,你可以通过像束重建你的类的对象
公共CustomClass(上下文_context,叠B){
上下文= _context;
classMember = b.getString(SomeKey);
}
I've got a few custom objects like RootObject
and Form
that I want to pass on to the next activity.
This is an example of RootObject
:
public class RootObject
{
public Form Form { get; set; }
}
But how can I pass RootObject
to the next activity with an Intent
and get it in the next Activity
? In Form
there are again multiple properties with Lists
and stuff and I need to access some of the properties in my next Activity
. My intent is called like this:
saveButton.Click += delegate {
if(ValidateScreen()){
SaveData();
Intent intent = new Intent(this, typeof(MainActivity));
Bundle b = new Bundle();
b.PutSerializable("RootObject", RootObject);
StartActivity(intent);
}
};
解决方案
This is how you can go about it. Your class needs to implement Serializable or Parcelable. In the first Activity(where you want to send from):
final Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("my_class", your_object);
startActivity(intent);
Then in your second Activity, you would do:
final Intent passedIntent = getIntent();
final YourClass my_class = (YourClass) passedIntent.getSerializableExtra("my_class");
There is also another way of doing it, using Bundles::
Create a Bundle from your class like:
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString("SomeKey", "SomeValue");
return b;
}
Then pass this bundle with INTENT. Now you can recreate your class object by passing bundle like
public CustomClass(Context _context, Bundle b) {
context = _context;
classMember = b.getString("SomeKey");
}