gpt4 book ai didi

android - 保存 Arraylist 用于旋转后显示

转载 作者:行者123 更新时间:2023-11-29 18:07:48 24 4
gpt4 key购买 nike

我可以保留ArrayList<HashMap<String, String>>在 bundle ?

我要显示ListView旋转屏幕后立即。

最佳答案

您不必将其保存在 Bundle 中。事实上,在 Bundle 中存储的信息似乎很多。

因为你只是保存一个 String 对象的 HashMap,你可以创建一个类来为你缓存它或者让它成为一个 static类中的字段。

静态字段可以很好地使用,只要它们不包含对 ActivityViewDrawable< 的引用。 (这会导致内存泄漏)

最简单的方法可能就是:

private static HashMap<String,String> myMap = new HashMap<String,String>();

因为这个字段是static,所以当你重新创建 Activity 时它不会被重新创建。相反,它仍然会在那里供您使用相同的值。


更多存储数据的方式:

(这些技术也适用于通过配置更改共享数据)。

http://developer.android.com/guide/faq/framework.html#3

How do I pass data between Activities/Services within a single application?

It depends on the type of data that you want to share:

Primitive Data Types

To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.

Non-Persistent Objects

For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:

Singleton class

You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value. A public static field/method

An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.

A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.

Persistent Objects

Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.

For sharing complex persistent user-defined objects, the following approaches are recommended:

  • Application Preferences
  • Files
  • contentProviders
  • SQLite DB

If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders. Please refer to the Data Storage for further details on how to use these components.

关于android - 保存 Arraylist<Hashmap> 用于旋转后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12500508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com