Intent.putExtras大小限制?大小、Intent、putExtras

2023-09-12 23:00:31 作者:烟酒释情

我想通过 Intent.putExtras 这样通过从一个活动到另一个数据:

I'm trying to pass data from one activity to another via Intent.putExtras like this:

private ArrayList<HashMap<String, String>> mGroups = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> mUsers = new ArrayList<HashMap<String, String>>();
...

Bundle data = new Bundle();
data.putInt("mode", mode);
data.putSerializable("groups", (Serializable) mGroups);
data.putSerializable("users", (Serializable) mUsers);
data.putInt("current_class", mCurrentClassId);
data.putInt("current_user", mCurrentUserId);

Intent intent = new Intent(ctx, ChildActivity.class);
intent.putExtras(data);
ctx.startActivityForResult(intent, 0);

下面 mUsers 的HashMap℃的列表,字符串,字符串&GT; 与用户的数据,包括Base64的恩codeD照片,串之和尺寸在这个名单是500KB左右。

Here mUsers is a List of HashMap<String,String> with users' data, including Base64-encoded photo, sum of strings sizes in this list is about 500Kb

号召 startActivityForResult 挂了好几分钟,黑色屏幕,然后我得到ANR错误。子活动的的onCreate 不叫的。

Call to startActivityForResult hangs for several minutes with black screen and then I get ANR error. Sub-Activity's onCreate is not called at all.

如果我不加大型字符串到mUsers(无Base64的恩codeD照片) - 只是罚款

If I don't add large strings into mUsers (no Base64-encoded photos) - works just fine.

请帮忙。

推荐答案

如果这两个活动都是你的,用一个体面的数据模型。 Android不鼓励太多设计得非常好应用。或者把它不同,它允许快速developped应用程序,不提倡多优秀的软件应用原理。

if both activities are yours, use a decent data model. Android doesn't encourage that much to very well designed application. Or turn it differently, it allows for fast developped application and doesn't promote much of good software application principle.

的@该解决方案让 - 菲利普·罗伊(魁北克?)是有趣的,但单是相当的反模式,当涉及到更复杂的东西,也就是有状态的模型或服务。

The solution of @Jean-Philippe Roy (québec ?) is interesting but singleton are quite an anti-pattern when it comes to more elaborate things, namely statefull models or serviceS.

最好的办法是使用一个应用程序类。这个类是你的单,通过自然的机器人。因此,

The best option is to use an application class. This class is your singleton, by nature in android. So,

在清单定义一个应用程序类 提供了一个静态方法来访问应用程序类的唯一实例(它始终是一个单例)。 给它一个方法来接收和保存数据,从您的第一项活动称之为 ,第二个,让他们回到你的第二个活动 define an application class in your manifest provide a static method to access the unique instance of the application class (it is always a singleton). give it a method to receive and hold your data, call it from your first activity and a second one to get them back in your second activity

---更新后@ straya的回答和18个月的Andr​​oid编程:)

---Updated after @straya's answer and 18 more month of Android programming :)

共享数据结构或进程翻过应用程序,活动的意见,碎片的问题始终是构建Android应用程序时,present在心中。重要的是要了解和考虑的应用范围是正确的地方持有共享结构,但使用应用程序类本身把一个数据结构的范围是不可行的问候:

The question of sharing a data structure or processes accross application, activities, views, fragments is always present at mind when building Android application. It's important to know and consider that the application scope is the right place to hold shared structure, but using the application class itself to put a data structure in that scope is not viable with regards to :

code的质量,如果所有共享的数据结构和流程都知道应用程序,它会很快变得臃肿访问所有这些实体。 在有实体只有一个全局共享池,这是没有找到足够的细粒,并可能导致防不胜防耦合实体的方式

我使用依赖注入管理的单身人士现在倾向于preFER。匕首或RoboGuice都允许创建和注入的给定类的一个实例为其他类。这种技术和DI更普遍提供了良好的Andr​​oid的设计极大的可能性:

I now tend to prefer using Dependency Injection managed singletons. Dagger or RoboGuice both allow to create and inject a single instance of a given class into other classes. This technique, and DI more generally offers great possibilities for good Android designs :

在不降低了code的质量,它甚至缩短了不少。使用@注入注入依赖,他们将获得注入。 请不要给2责任落实到singletoned类:它不会处理单实例的创建,该框架将做到这一点 更容易从单传递到一个正常的实例 在那些单身趋于正常班,一个简单的注解,它们不包含静态方法了,这让非常轻松地嘲笑他们。这是一个很大的问题。 当然,DI注释非常清楚当一个类依赖于另一个类,有助于自我文件code以上。