开发者

Regarding Intents

According to what i have learnt from passing data using Intents is that when you pass Object O from Activity A to Activity B via intents, activity B receives a COPY of object O. The way things work is that The object O gets serialized (converted to a sequence of bytes) and that sequence of bytes is then passed to Activity B. Then activity B recreates a copy of object O at the moment it was serialized. I would like to know if it would be efficient if one extends the Intent class to create a custom Intent and have references to the objects that are required by the other activities and pass the data to the other activities. For example:

public class CustomIntent extends Intent {
private Object o;

public CustomIntent() {
    super();
    // TODO Auto-generated constructor stub
}开发者_如何学编程
public Object getObject () {
    return o;
}
public void setObject(Object object) {
    this.o = object;
} 
}

In the receiving activity i get the intent and cast the intent to the CustomIntent type and retrieve the object required by the activity. Would this improve the efficiency by reducing the need for Serialization? Kindly throw some light on this. Thanks in advance.


No. Intents are dispatched by the Android system and are always serialized as they can be sent to any activity, service, etc in the system.

For your problem you could probably workaround this issue by creating an Application class and storing your data in it:

class CustomApplication extends Application {
  private Object data;

  public Object getData() {
    return data;
  }

  public void setData(Object data) {
    this.data = data;
  }
}

You activate it by updating AndroindManifest.xml setting the android:name property on the application tag you your class name.

To use in your activities:

CustomApplication app = (CustomApplication) getApplicationContext();
app.setData(yourDataObject);


I think it would be better if you let the android handle everything for you. Do not customize it, if it is not very essential.

If you want to have the reference of the object in another activity then there are other ways too.

  • You can make your object static and directly access it from other activity.

  • You can make a new object of same type and replace it after coming again back to the first activity(in onActivitResult() method.).

or there may be many more ways to do it.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜