我如何通过一个对象从一个活动到另一个在Android?

我如何通过一个对象从一个活动到另一个在Android?

问题描述:

我需要能够使用在多个活动之一,目的在我的应用程序,它需要的的一样的对象。什么是做到这一点的最好方法是什么?

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?

当你创建了意向性对象时,可以采取以下两种方法的优势 两个活动之间传递对象。

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

putParceble

putSerializable

您可以有你的类实现或者 Parcelable 或的序列化。然后你就可以绕过你的自定义类的各种活动。我发现这非常有用。

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.

下面是code一个小片段,我使用

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);

和新开工活动code会是这样的...

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);