Issue: Passing large data to second Activity

前端 未结 7 1509
别那么骄傲
别那么骄傲 2020-11-30 01:53

I\'ve got an strange issue. I was looking around the web but didn\'t find an answer. I\'m still a beginner in android programming. So let\'s go:

All I want to do is

相关标签:
7条回答
  • 2020-11-30 02:29

    I faced this TransactionTooLargeException issue recently and was looking for a possible solution to avoid it. At last, I couldn't find anything which would work. In most of the answers, you will find people recommending different ways to avoid this exception, but a proper example wasn't available.

    Here's what I have done to fix this issue, this might not be an ideal solution at a few places and has some limitation as well.

    Step 1 - Write a class which will convert the bundle into a string and store it in Shared Preference.

    public class ActivityBridge {
    
        private static final String KEY_ACTIVITY_BRIDGE = "ACTIVITY_BRIDGE";
        private final Context context;
        private SharedPreferences sharedPreferences;
    
    
        public ActivityBridge(Context context) {
            this.context = context;
    
            sharedPreferences = context.getSharedPreferences(KEY_ACTIVITY_BRIDGE, Context.MODE_PRIVATE);
        }
    
    
        @SuppressLint("ApplySharedPref")
        public void putData(Bundle bundle, Intent intent) {
            sharedPreferences.edit()
                    .putString(
                            intent.toString(),
                            Base64.encodeToString(bundleToBytes(bundle), 0)
                    )
                    .commit();
        }
    
    
        @SuppressLint("ApplySharedPref")
        public Bundle getData(Intent intent) {
            Bundle bundle;
            final String bundleString = sharedPreferences.getString(intent.toString(), "");
    
            if (TextUtils.isEmpty(bundleString)) {
                return null;
            } else {
                bundle = bytesToBundle(Base64.decode(bundleString, 0));
            }
    
            sharedPreferences.edit()
                    .clear()
                    .commit();
    
            return bundle;
        }
    
    
        public byte[] bundleToBytes(Bundle bundle) {
            Parcel parcel = Parcel.obtain();
            parcel.writeBundle(bundle);
            byte[] bytes = parcel.marshall();
            parcel.recycle();
            return bytes;
        }
    
    
        public Bundle bytesToBundle(byte[] bytes) {
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(bytes, 0, bytes.length);
            parcel.setDataPosition(0);
            Bundle bundle = parcel.readBundle(context.getClassLoader());
            parcel.recycle();
            return bundle;
        }
    }
    

    Step 2 - Usage

    While creating Intent

             Intent intent = new Intent(ActivityA.this, ActivityB.class);
    
             Bundle bundle = new Bundle();
             bundle.putString("<KEY>", "<VALUE>");
             new ActivityBridge(ActivityA.this).putData(bundle, intent);
    
             startActivity(intent);
    

    while extracting Bundle

        Bundle bundle = new ActivityBridge(this).getData(getIntent());
    

    Note: This solution clears the stored Bundle after being read and will not return the Bundle if the Activity is recreated. This is a workaround and any suggestion or issues will be highly appreciated.

    0 讨论(0)
提交回复
热议问题