简体   繁体   English

Android使用Parcelable在活动之间发送数据

[英]Android use Parcelable to send data between activities

In my application I want to pass an ArrayList of Parcelable objects (or a Parcelable[] ) between two activities. 在我的应用程序中,我想在两个活动之间传递一个Parcelable对象的ArrayList (或Parcelable[] )。 On the first activity i call a restservice to get the json data, after that I use gson to get the List of Parcelable object and then i send the list to the second activity using putParcelableArrayListExtra(...) . 在第一个活动我叫restservice得到JSON数据,在那之后我用GSON拿到ListParcelable对象,然后我送名表给使用第二活性putParcelableArrayListExtra(...) All works fine until the size of the list is about 1000, but over this size I get an ANR and application won't resume even if I wait for some minutes. 在列表的大小大约为1000之前,所有方法都可以正常工作,但是在此大小以上,我会得到ANR,即使我等待几分钟,应用程序也不会恢复。

Is there a solution for this issue maintaining this approach? 维持这种方法是否有解决此问题的解决方案? Is it due to a size limit for object put to an Intent? 是否由于放入Intent的对象的大小限制?

I know I could pass the Json String and then get the list on the second activity, but I prefer to make first this kind of operations. 我知道我可以传递Json String,然后在第二个活动中获取列表,但我更喜欢先进行此类操作。

First off, ensure that the source of your ANR is indeed the large parcelable object, and not because you were performing a blocking operation (ie networking) on the main thread. 首先,请确保您的ANR的来源确实是可打包的大对象,而不是因为您在主线程上执行了阻塞操作(即联网)。

Now if the ANR issue really is due to these large objects, the right way to get this done is to deserialize the object and write it out to storage instead of passing it between activities. 现在,如果ANR问题确实是由于这些大对象引起的,那么完成此任务的正确方法是将对象反序列化并将其写出到存储中, 而不是在活动之间传递它。 You're effectively doubling the amount of memory you incur by doing what you're doing. 通过做您正在做的事情,实际上可以使您所占用的内存量加倍。

Here are a few ways you can troubleshoot this: 您可以通过以下几种方法来解决此问题:

  • StrictMode: StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. StrictMode: StrictMode最常用于在应用程序的主线程上捕获意外的磁盘或网络访问,在该主线程上接收UI操作并进行动画处理。 By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users. 通过使应用程序的主线程保持响应状态,还可以防止向用户显示ANR对话框。

  • Traceview: Traceview is a graphical viewer to see logs created by an Android application. Traceview: Traceview是一个图形查看器,用于查看由Android应用程序创建的日志。 Via Traceview you can find errors in your application and measure its performance. 通过Traceview,您可以找到应用程序中的错误并评估其性能。

  • Memory Dump: You can create a memory snapshot and analyse it with the Eclipse Memory Analyzer. 内存转储:您可以创建内存快照并使用Eclipse内存分析器对其进行分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM