java 在活动之间传递自定义对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5621132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing custom objects between activities?
提问by Skizit
How do I pass custom objects between activites in android? I'm aware of bundles but I can't seem to see any functionality for this in them. Could anyone show me a nice example of this?
如何在android中的活动之间传递自定义对象?我知道捆绑包,但我似乎看不到其中的任何功能。谁能告诉我一个很好的例子?
回答by Igor Filippov
回答by Arun kumar
Using Parcelable interface you can pass custom java object into the intent.
使用 Parcelable 接口,您可以将自定义 java 对象传递到意图中。
1) implement the Parcelable interface to your class like:
1) 为您的类实现 Parcelable 接口,例如:
class Employee implements Parcelable
{
}
2) Pass the Parcelable object into the intent like:
2)将 Parcelable 对象传递到意图中,例如:
Employee mEmployee =new Employee();
Intent mIntent = new Intent(mContect,Abc.class);
mIntent.putExtra("employee", mEmployee);
startActivity(mIntent);
3) Get the data into the new [Abc] Activity like:
3) 将数据放入新的 [Abc] Activity 中,例如:
Intent mIntent = getIntent();
Employee mEmployee = (Employee )mIntent.getParcelableExtra("employee");
回答by tony gil
a Parcel
MIGHT solve your problem.
可能会Parcel
解决您的问题。
think of a Parcel
as an "array" (metaphorical) of primitive types (long, String, Double, int, etc). if your custom classis composed of primitive types ONLY, then change your class declaration including implements Parcelable
.
将 aParcel
视为原始类型(long、String、Double、int 等)的“数组”(隐喻)。 如果您的自定义类仅由原始类型组成,则更改您的类声明,包括implements Parcelable
.
you can pass a parcelable object thru an intent with no difficulty whatsoever (just like you would send a primitive-typed object). in this case i have a parcelable custom classcalled FarmData(composed of longs, strings and doubles) which i pass from one activity to another via intent.
您可以毫无困难地通过 Intent 传递 Parcelable 对象(就像发送原始类型对象一样)。在这种情况下,我有一个名为FarmData的可打包自定义类(由 longs、strings 和 doubles 组成),我通过意图从一个活动传递到另一个活动。
FarmData farmData = new FarmData();
// code that populates farmData - etc etc etc
Intent intent00 = new Intent(getApplicationContext(), com.example.yourpackage.yourclass.class);
intent00.putExtra("farmData",farmData);
startActivity(intent00);
but retrieving it may be tricky. the activity that receives the intent will check if a bundle of extras was send along with the intent.
但检索它可能很棘手。接收到意图的活动将检查是否与意图一起发送了一组额外的内容。
Bundle extras = getIntent().getExtras();
FarmData farmData = new FarmData();
Intent intentIncoming = getIntent();
if(extras != null) {
farmData = (FarmData) intentIncoming.getParcelableExtra("farmData");// OK
}
回答by JAL
Given an object PasswordState that implements Serializable throughout the object tree, you can pass this object to anther activity as in:
给定一个在整个对象树中实现 Serializable 的对象 PasswordState,您可以将此对象传递给另一个活动,如下所示:
private void launchManagePassword() {
Intent i= new Intent(this, ManagePassword.class); // no param constructor
PasswordState outState= new PasswordState(lengthKey,timeExpire,isValidKey,timeoutType,"",model.getIsHashPassword());
Bundle b= new Bundle();
b.putSerializable("jalcomputing.confusetext.PasswordState", outState);
i.putExtras(b);
startActivityForResult(i,REQUEST_MANAGE_PASSWORD); // used for callback
}
回答by Grégori Fernandes de Lima
One simple way to pass an object between activities or make a object common for all applicattion, is create a class extending Application.
在活动之间传递对象或使对象对所有应用程序通用的一种简单方法是创建一个扩展应用程序的类。
Here is a example:
下面是一个例子:
public class DadosComuns extends Application{
private String nomeUsuario="";
public String getNomeUsuario() {
return nomeUsuario;
}
public void setNomeUsuario(String str) {
nomeUsuario = str;
}
}
In all your others activities, you just need instantiate one object "DadosComuns", declarating as a Global Variable.
在所有其他活动中,您只需要实例化一个对象“DadosComuns”,声明为全局变量。
private DadosComuns dadosComuns;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//dados comuns
dadosComuns = ((DadosComuns)getApplicationContext());
dadosComuns.setNomeUsuario("userNameTest"); }
All others activities that you instantiate dadosComuns = ((DadosComuns)getApplicationContext());you can acess getNomeUsuario() == "userNameTest"
您实例化的所有其他活动 dadosComuns = ((DadosComuns)getApplicationContext()); 你可以访问getNomeUsuario() == "userNameTest"
In your AndroidManifest.xmlyou need to have
在你的AndroidManifest.xml你需要有
<application
android:name=".DadosComuns"