相关疑难解决方法(0)

android:support:fragments 保存的 Activity 越来越大导致 TransactionTooLargeException

我有一个控制多个片段的活动。默认片段是HomepageFragment. 当替换到不同的 Fragment 时,我总是确保堆栈保持平坦 - 这意味着只有HomepageFragment保持在堆栈中并在其顶部是当前 Fragment。例如:

  1. 活动开始于 HomepageFragment
  2. FragmentA需要替换 to - 一切都是好的原因HomepageFragment是最后一个 Fragment
  3. 现在堆栈是HomepageFragment->FragmentA
  4. FragmentB需要替换为- 首先弹出堆栈上的最后一个 Fragment ( fragmentA) 然后替换为FragmentB
  5. 现在堆栈是HomepageFragment->FragmentB

在生产中,我看到很多TransactionTooLargeException崩溃。

我曾经TooLargeTool跟踪问题的来源,我发现当我在活动中的片段之间切换时,其中有一个android:support:fragments键SaveInstanceState变得越来越大(呈指数级),直到发生崩溃。

似乎即使从堆栈中弹出时,有关原始事务的一些数据也会继续保存。

按照此处的建议将其删除会导致 Activity 在被操作系统杀死后无法正确恢复。

我压平堆栈的方法有什么问题吗?有没有更好的方法?究竟保存在什么数据下android:support:fragments?

注意:我没有为这些片段设置任何参数。此外,它们在其 saveInstanceState 包中保存了非常小的数据。

谢谢!

android android-fragments android-activity onsaveinstancestate transactiontoolargeexception

11
推荐指数
1
解决办法
886
查看次数

即使文件大小超小,TransactionTooLargeException也是如此

尝试共享图像文件时,我不断收到此错误:

java.lang.RuntimeException:android.os.TransactionTooLargeException:数据包大小1085992字节

我假设一个解决方案是将图像压缩得更多,这会减小尺寸.这是完成这项工作的功能:

 public static File saveBitmaptoFile(Bitmap bitmap, File pictureFile) {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pictureFile);

        // on the next line I'm trying compress the heck out of image.
        bitmap.compress(Bitmap.CompressFormat.JPEG, 1, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return pictureFile;
}
Run Code Online (Sandbox Code Playgroud)

这是共享功能:

 private void shareToInstagram() {
        String type = "image/png";
        Intent share = new Intent(Intent.ACTION_SEND); …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-sharing

0
推荐指数
1
解决办法
5120
查看次数