TransactionTooLargeException in Nougat

后端 未结 4 991
故里飘歌
故里飘歌 2021-01-06 00:57

Exception

 05-12 15:42:45.791 11043-11043/ E/UncaughtException: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 631792         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 01:32

    Your are passing too much data to your Fragment in setArguments(). Your Fragment will work, but when it tries to save its instance state it overflows the transaction buffer. This throws a RuntimeException if you target Android 7.0 (API 24 or higher). To preserve backwards compatibility and not break existing apps, the new behaviour is only used if you target API 24 or higher. If you target API < 24, the transaction buffer overflow exception is caught and silently ignored. This means that your data will not be persistently saved, which you may (or may not) notice.

    Your code is broken. You should not pass large amounts of data to the Fragment in setArguments(). You can keep your data in your Activity. When the Fragment wants to access the data, it can always so something like this:

    // Get the owning Activity
    MyActivity activity = (MyActivity)getActivity();
    // Get the data from the Activity
    List data = activity.getData();
    

    In your Activity, write a getData() method that returns a reference to whatever data the Fragment needs.

    In this way, the data is held in the Activity and the Fragment can get access to it whenever it needs to.

提交回复
热议问题