Android中活动之间的对象共享

编程入门 行业动态 更新时间:2024-10-25 12:25:34
本文介绍了Android中活动之间的对象共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我有一个关于在整个应用程序中传递对象的问题.假设我想在整个应用程序中拥有一个大型自定义对象.该对象将由多个活动和服务使用.我起初是这样做的.

Hi I have a question about passing an object throughout the app. Say I want to have a big custom object throughout the app. This object will be used by multiple activities and services. What I did at first is this.

首先,我创建了一个Application类并定义了一个单例对象.

First, I created an Application class and define a singleton object.

public class FooApplication extends Application { public static SharedObj obj; ... }

第二,在一个活动中,我设置了该对象的值

Second, in an activity I set a value of this object

public class FooActivity extends Activity { OnCreate() { FooApplication.obj = SharedObj.getInstance(); } }

第三,在另一个活动中,我访问该对象

Third, in another activity I access this object

public class BarActivity extends Activity { OnCreate() { SharedObj useThis = FooApplication.obj; } }

我的问题是,这有什么问题?似乎工作正常,但我发现有时由于某些原因将此单例对象的值设置为null.我像这样共享此对象而不是使其成为可拆分对象的主要原因是,这样做对我来说有点昂贵,而且我所做的事情看起来很容易做到.有缺点吗?

My question is, what is wrong with this? It seems to work fine but I find that sometimes the value of this singleton object is set to null for some reason. The primary reason I am sharing this object like this instead of making it parcelable is that doing so is kinda expensive for me and what I did looks much easy to do. Is there a downside?

经过研究,我发现还有另一种在整个应用程序中共享对象的方法.

After some research I found there is another way to share an object throughout the app.

首先,在应用程序类中定义

First, define in app class

public class FooApplication extends Application { public SharedObj obj = new SharedObj(); ... }

第二,在这样的活动中初始化

Second, initialize in an activity like this

public class FooActivity extends Activity { OnCreate() { FooApplication fooApp = (FooApplication)getApplicationContext(); fooApp.obj = new SharedObj(); } }

第三,在另一个活动中,我访问该对象

Third, in another activity I access this object

public class BarActivity extends Activity { OnCreate() { SharedObj useThis = ((FooApplication)getApplicationContext()).obj; } }

此方法(使用getapplicationContext())与像在第一部分中一样使用单例对象有何不同?这是更推荐吗?

How is this method (using getapplicationContext()) different from using a singleton object like I did in the first section? Is this more recommended?

提前谢谢!

推荐答案

此方法的主要缺点是Android系统可能会在任何时间终止您的进程.通常,当需要将内存用于其他目的(例如打入电话)时,可以执行此操作.然后,Android将重新启动您的流程,并重新创建每个活动(当活动可见时).这样,它将调用每个活动的onCreate方法,并向其传递该活动已保存的打包数据包.

The main downside to this approach is the fact that the Android system may kill your process at any point in time. Typically this is done when memory is needed for other purposes (like an incoming phone call for example). Android will then re-start your process, and re-create each activity (when the activity becomes visible). In doing so it will call each activity's onCreate method, passing it a bundle of the parcelized data that the activity has saved.

也将重新创建应用程序类.但是,您在那里保存的对象将不会被重新创建.

The application class will be re-created as well. The object you have saved there however will not be re-created.

因此,尽管痛苦不堪,但打包确保了您的对象可以恢复为活动(和流程)被杀死时的状态.

Thus, as painful as it is, parcelization ensures that your object can be resorted to the state it was in when the activity (and the process) was killed.

另一种方法是自己保留对象.使用SharedPreferences是一种常见的方法.

Another approach would be to persist the object yourself. Using SharedPreferences is one common approach.

====

因此,总而言之,我建议通过Intent在活动之间传递对象.重新创建活动时,用于启动活动的意图总是被完全重新构造,即使在进程终止/重新启动的情况下也是如此.

So in summary, I suggest passing the object from activity to activity via Intent. The intent that is used to launch an activity is always fully re-constituted when that activity is re-created, even in the case of a process kill/re-launch.

更多推荐

Android中活动之间的对象共享

本文发布于:2023-08-01 12:26:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!