简体   繁体   English

暂停活动后的TransactionTooLargeException

[英]TransactionTooLargeException when Activity is Paused

I am creating an application and in there I am sending very big data in Bundle from Service to the Activity . 我正在创建一个应用程序,并在其中将Bundle大数据从Service发送到Activity But I am not using Messenger for that. 但是我没有使用Messenger Instead, I am assigning data to a variable in singleton class and then I am retrieving data from that singleton class and assigning it to the activity. 相反,我将数据分配给单例类中的变量,然后从那个单例类中检索数据并将其分配给活动。

In the Activity , I have a ViewPager in which two fragments are inflated. Activity ,我有一个ViewPager ,其中两个片段都被ViewPager了。 The Bundle is being sent to both of these Fragment classes. Bundle发送到这两个Fragment类。

Now, when the activity is paused, application is throwing following exception: 现在,当活动暂停时,应用程序将引发以下异常:

at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4209)
                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                     Caused by: android.os.TransactionTooLargeException: data parcel size 4538408 bytes
                                                                        at android.os.BinderProxy.transactNative(Native Method)
                                                                        at android.os.BinderProxy.transact(Binder.java:628)
                                                                        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:4183)
                                                                        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4201)
                                                                        at android.os.Handler.handleCallback(Handler.java:751) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

Please help. 请帮忙。

The bundle is being sent to both of these fragment classes. 捆绑包将发送到这两个片段类。

Do not put large things into the saved instance state Bundle . 不要将大东西放入保存的实例状态Bundle In the case of fragments, the "arguments" Bundle ( setArguments() ) becomes part of the saved instance state Bundle , and so do not put large things into the arguments Bundle . 对于片段,“参数” BundlesetArguments() )成为已保存实例状态Bundle ,因此请勿在参数Bundle放入大内容。 The saved instance state Bundle is for tiny bits of data: as little as possible, but enough for you to restore your UI state after your process is terminated. 保存的实例状态Bundle用于存储少量数据:尽可能少,但是足以让您在进程终止后恢复UI状态。

Instead, do something else, such as: 相反,请执行其他操作,例如:

  • Have the fragments call methods on the hosting activity to get this data, or 在托管活动中使用片段调用方法来获取此数据,或者
  • Use a ViewModel , so that this data is retained across configuration changes but is not part of the saved instance state Bundle 使用ViewModel ,以便在配置更改时保留这些数据,但不属于已保存实例状态Bundle

From Android Developers - TransactionTooLargeException 来自Android开发人员-TransactionTooLargeException

The Binder transaction buffer has a limited fixed size, currently 1Mb Binder事务缓冲区的固定大小有限,目前为1Mb

And from similar question: What to do on TransactionTooLargeException 并从类似的问题: 在TransactionTooLargeException上做什么

Actually data size was around 500kb, and the IPC transaction buffer size is set to 1024KB. 实际上,数据大小约为500kb,而IPC事务缓冲区大小设置为1024KB。

So, data parcel size 4538408 bytes (4MB) is too large to send at once. 因此,数据包大小4538408字节(4MB)太大,无法立即发送。 Split it. 拆分它。

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

相关问题 暂停时由Activity实例化的对象会发生什么 - What happens to the objects Instantiated by Activity when it is paused 使用LifeCycleOwner参数初始化的Firestore侦听器在活动暂停时错过更新 - Firestore listener initialized with LifeCycleOwner argument misses updates when activity is paused 活动没有足够快地暂停 - Activity not paused quickly enough 调用 TakePicture 时出现 Android TransactionTooLargeException - Android TransactionTooLargeException when calling TakePicture 执行queryIntentActivities时android.os.TransactionTooLargeException - android.os.TransactionTooLargeException when executing queryIntentActivities 传递Parcelable ArrayList时,Android TransactionTooLargeException - Android TransactionTooLargeException when passing a Parcelable ArrayList 从 Fragment 移动到 Activity 导致 android.os.TransactionTooLargeException - Move from Fragment to Activity Causes android.os.TransactionTooLargeException 暂停活动后,如何在倒数计时器中正确停止Media Player? - How do I correctly stop a Media Player in a CountDown Timer when the activity is Paused? 当活动被破坏/暂停时,我应该删除任何监听器吗? Android工作室 - Should I remove any listeners when activity is destroyed/paused? Android Studio 如何在Android中将消息发送到暂停的活动 - How to send message to paused activity in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM