Matan
Matan

Reputation: 179

Move from Fragment to Activity Causes android.os.TransactionTooLargeException

I'm trying to move from a Fragment to an Activity using Intents, and where I run my code, it causes android.os.TransactionTooLargeException.

My Fragment code (CardFragment.java):

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtName = view.findViewById(R.id.txt_name);
    btnFood = view.findViewById(R.id.btn_food);

    txtName.setText(mName);

    Bitmap bm = StringToBitMap(mImage);
    btnFood.setImageBitmap(bm);

    btnFood.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), Recipes.class);
            intent.putExtra("Name", mName);
            intent.putExtra("Recipe", mRecipe);
            intent.putExtra("Image", mImage);
            startActivity(intent);
        }
    });
}

The mName, mRecipe and mImage are String variables.

My Activity code (Recipes.java):

public class Recipes extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipes);

        TextView txtName = findViewById(R.id.txt_name);
        TextView txtRecipe = findViewById(R.id.txt_recipe);
        ImageView imgView = findViewById(R.id.img_view);

        txtName.setText(getIntent().getStringExtra("Name"));
        txtRecipe.setText(getIntent().getStringExtra("Recipe"));

        Bitmap bm = CardFragment.StringToBitMap(getIntent().getStringExtra("Image"));
        imgView.setImageBitmap(bm);
    }
}

Every time I run this code, I get this error message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.recipeapp, PID: 12160
    java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:540)
        at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4408)
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

What is this error and how can I solve it? Maybe because there are multiple CardFragments in my previous view? Thanks for the help.

Upvotes: 0

Views: 1906

Answers (1)

special N9NE
special N9NE

Reputation: 21

This error means you sending more than 1 MB of data through intent, in this case, your bitmap is too large for intent.

The solution is to reduce bitmap size or use different ways to pass bitmap like services, etc.

Upvotes: 0

Related Questions