如何在Android上将一个对象从一个活动传递到另一个活动?(复制)

时间:2022-02-12 21:13:08

This question already has an answer here:

这个问题已经有了答案:

I need to be able to use one object in multiple activities within my app, and it needs to be the same object. What is the best way to do this?

我需要能够在我的应用的多个活动中使用一个对象,它需要是同一个对象。最好的方法是什么?

I have tried making the object "public static" so it can be accessed by other activities, but for some reason this just isn't cutting it. Is there another way of doing this?

我尝试过将对象设置为“public static”,以便其他活动可以访问它,但是出于某种原因,这并没有切断它。还有别的办法吗?

6 个解决方案

#1


135  

When you are creating an object of intent, you can take advantage of following two methods for passing objects between two activities.

当您创建意图对象时,您可以利用以下两种方法在两个活动之间传递对象。

putParcelable

putParcelable

putSerializable

putSerializable

You can have your class implement either Parcelable or Serializable. Then you can pass around your custom classes across activities. I have found this very useful.

您可以让类实现Parcelable或Serializable。然后,您可以在活动之间传递自定义类。我发现这很有用。

Here is a small snippet of code I am using

下面是我正在使用的一小段代码

CustomListing currentListing = new CustomListing();
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.CUSTOM_LISTING, currentListing);
i.putExtras(b);
i.setClass(this, SearchDetailsActivity.class);
startActivity(i);

And in newly started activity code will be something like this...

在新开始的活动代码中会是这样的…

Bundle b = this.getIntent().getExtras();
if (b != null)
    mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);

#2


36  

You can create a subclass of Application and store your shared object there. The Application object should exist for the lifetime of your app as long as there is some active component.

您可以创建应用程序的子类并将共享对象存储在那里。只要存在活动组件,应用程序对象就应该在应用程序的整个生命周期中存在。

From your activities, you can access the application object via getApplication().

从您的活动中,您可以通过getApplication()访问应用程序对象。

#3


35  

This answer is specific to situations where the objects to be passed has nested class structure. With nested class structure, making it Parcelable or Serializeable is a bit tedious. And, the process of serialising an object is not efficient on Android. Consider the example below,

这个答案是特定于要传递的对象具有嵌套类结构的情况。使用嵌套类结构,使它可分割或可序列化有点乏味。而且,在Android上序列化一个对象的过程并不高效。考虑下面的例子,

class Myclass    {
  int a;
  class SubClass    {
       int b;
  }
}

With Google's GSON library, you can directly parse an object into a JSON formatted String and convert it back to the object format after usage. For example,

使用谷歌的GSON库,您可以直接将对象解析为JSON格式的字符串,并在使用后将其转换为对象格式。例如,

MyClass src = new MyClass();
Gson gS = new Gson();
String target = gS.toJson(src); // Converts the object to a JSON String

Now you can pass this String across activities as a StringExtra with the activity intent.

现在,您可以将这个字符串作为带有活动意图的StringExtra传递给活动。

Intent i = new Intent(FromActivity.this, ToActivity.class);
i.putExtra("MyObjectAsString", target);

Then in the receiving activity, create the original object from the string representation.

然后在接收活动中,从字符串表示中创建原始对象。

String target = getIntent().getStringExtra("MyObjectAsString");
MyClass src = gS.fromJson(target, MyClass.class); // Converts the JSON String to an Object

It keeps the original classes clean and reusable. Above of all, if these class objects are created from the web as JSON objects, then this solution is very efficient and time saving.

它使原始类保持干净和可重用。最重要的是,如果这些类对象是作为JSON对象从web创建的,那么这个解决方案是非常高效和节省时间的。

UPDATE

更新


While the above explained method works for most situations, for obvious performance reasons, do not rely on Android's bundled-extra system to pass objects around. There are a number of solutions makes this process flexible and efficient, here are a few. Each has its own pros and cons.

虽然上述解释的方法适用于大多数情况,但出于明显的性能原因,不要依赖于Android的绑定额外系统来传递对象。有许多解决方案可以使这个过程变得灵活和高效,以下是其中的一些。它们各有利弊。

  1. Eventbus
  2. Eventbus
  3. Otto
  4. 奥托

#4


6  

Maybe it's an unpopular answer, but in the past I've simply used a class that has a static reference to the object I want to persist through activities. So,

也许这是一个不受欢迎的答案,但在过去,我只是使用了一个类,这个类对我希望通过活动持久化的对象有一个静态引用。所以,

public class PersonHelper
{
    public static Person person;
}

I tried going down the Parcelable interface path, but ran into a number of issues with it and the overhead in your code was unappealing to me.

我尝试沿着可分割的接口路径走,但是遇到了许多问题,您的代码中的开销对我没有吸引力。

#5


5  

It depends on the type of data you need access to. If you have some kind of data pool that needs to persist across Activitys then Erich's answer is the way to go. If you just need to pass a few objects from one activity to another then you can have them implement Serializable and pass them in the extras of the Intent to start the new Activity.

这取决于您需要访问的数据类型。如果你有某种数据池需要跨activity,那么Erich的答案是。如果您只需要将一些对象从一个活动传递到另一个活动,那么您可以让它们实现Serializable,并将它们作为启动新活动的意图的附加部分传递给它们。

#6


4  

Your object can also implement the Parcelable interface. Then you can use the Bundle.putParcelable() method and pass your object between activities within intent.

您的对象还可以实现可共享接口。然后可以使用bundl . putparcelable()方法,并在意图中的活动之间传递对象。

The Photostream application uses this approach and may be used as a reference.

Photostream应用程序使用这种方法,可以用作参考。

#1


135  

When you are creating an object of intent, you can take advantage of following two methods for passing objects between two activities.

当您创建意图对象时,您可以利用以下两种方法在两个活动之间传递对象。

putParcelable

putParcelable

putSerializable

putSerializable

You can have your class implement either Parcelable or Serializable. Then you can pass around your custom classes across activities. I have found this very useful.

您可以让类实现Parcelable或Serializable。然后,您可以在活动之间传递自定义类。我发现这很有用。

Here is a small snippet of code I am using

下面是我正在使用的一小段代码

CustomListing currentListing = new CustomListing();
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.CUSTOM_LISTING, currentListing);
i.putExtras(b);
i.setClass(this, SearchDetailsActivity.class);
startActivity(i);

And in newly started activity code will be something like this...

在新开始的活动代码中会是这样的…

Bundle b = this.getIntent().getExtras();
if (b != null)
    mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);

#2


36  

You can create a subclass of Application and store your shared object there. The Application object should exist for the lifetime of your app as long as there is some active component.

您可以创建应用程序的子类并将共享对象存储在那里。只要存在活动组件,应用程序对象就应该在应用程序的整个生命周期中存在。

From your activities, you can access the application object via getApplication().

从您的活动中,您可以通过getApplication()访问应用程序对象。

#3


35  

This answer is specific to situations where the objects to be passed has nested class structure. With nested class structure, making it Parcelable or Serializeable is a bit tedious. And, the process of serialising an object is not efficient on Android. Consider the example below,

这个答案是特定于要传递的对象具有嵌套类结构的情况。使用嵌套类结构,使它可分割或可序列化有点乏味。而且,在Android上序列化一个对象的过程并不高效。考虑下面的例子,

class Myclass    {
  int a;
  class SubClass    {
       int b;
  }
}

With Google's GSON library, you can directly parse an object into a JSON formatted String and convert it back to the object format after usage. For example,

使用谷歌的GSON库,您可以直接将对象解析为JSON格式的字符串,并在使用后将其转换为对象格式。例如,

MyClass src = new MyClass();
Gson gS = new Gson();
String target = gS.toJson(src); // Converts the object to a JSON String

Now you can pass this String across activities as a StringExtra with the activity intent.

现在,您可以将这个字符串作为带有活动意图的StringExtra传递给活动。

Intent i = new Intent(FromActivity.this, ToActivity.class);
i.putExtra("MyObjectAsString", target);

Then in the receiving activity, create the original object from the string representation.

然后在接收活动中,从字符串表示中创建原始对象。

String target = getIntent().getStringExtra("MyObjectAsString");
MyClass src = gS.fromJson(target, MyClass.class); // Converts the JSON String to an Object

It keeps the original classes clean and reusable. Above of all, if these class objects are created from the web as JSON objects, then this solution is very efficient and time saving.

它使原始类保持干净和可重用。最重要的是,如果这些类对象是作为JSON对象从web创建的,那么这个解决方案是非常高效和节省时间的。

UPDATE

更新


While the above explained method works for most situations, for obvious performance reasons, do not rely on Android's bundled-extra system to pass objects around. There are a number of solutions makes this process flexible and efficient, here are a few. Each has its own pros and cons.

虽然上述解释的方法适用于大多数情况,但出于明显的性能原因,不要依赖于Android的绑定额外系统来传递对象。有许多解决方案可以使这个过程变得灵活和高效,以下是其中的一些。它们各有利弊。

  1. Eventbus
  2. Eventbus
  3. Otto
  4. 奥托

#4


6  

Maybe it's an unpopular answer, but in the past I've simply used a class that has a static reference to the object I want to persist through activities. So,

也许这是一个不受欢迎的答案,但在过去,我只是使用了一个类,这个类对我希望通过活动持久化的对象有一个静态引用。所以,

public class PersonHelper
{
    public static Person person;
}

I tried going down the Parcelable interface path, but ran into a number of issues with it and the overhead in your code was unappealing to me.

我尝试沿着可分割的接口路径走,但是遇到了许多问题,您的代码中的开销对我没有吸引力。

#5


5  

It depends on the type of data you need access to. If you have some kind of data pool that needs to persist across Activitys then Erich's answer is the way to go. If you just need to pass a few objects from one activity to another then you can have them implement Serializable and pass them in the extras of the Intent to start the new Activity.

这取决于您需要访问的数据类型。如果你有某种数据池需要跨activity,那么Erich的答案是。如果您只需要将一些对象从一个活动传递到另一个活动,那么您可以让它们实现Serializable,并将它们作为启动新活动的意图的附加部分传递给它们。

#6


4  

Your object can also implement the Parcelable interface. Then you can use the Bundle.putParcelable() method and pass your object between activities within intent.

您的对象还可以实现可共享接口。然后可以使用bundl . putparcelable()方法,并在意图中的活动之间传递对象。

The Photostream application uses this approach and may be used as a reference.

Photostream应用程序使用这种方法,可以用作参考。