Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

AndroidAndroid IntentBundleParcelable

Android Problem Overview


Why bundle has getParcelableArrayList, getParcelable methods; but Intent has only putParcelableArrayListExtra method? Can I transmit only object<T>, not ArrayList of one element? Then, what is getParcelable for?

Android Solutions


Solution 1 - Android

Intent provides bunch of overloading putExtra() methods.

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

To get it from intent in another activity:

Foo foo = getIntent().getExtras().getParcelable("foo");

Hope this helps.

Solution 2 - Android

Parcelable p[] =getIntent().getParcelableArrayExtra("parcel");

Solution 3 - Android

Sender Activity:

val intent = Intent(this, RestaurantDetails::class.java)
        intent.putExtra(Constants.RESTAURANT, restaurant)
        startActivity(intent)

Receiver Activity:

        val restaurant = intent.getParcelableExtra<Restaurant>(Constants.RESTAURANT)

Solution 4 - Android

It is important to remember that your models must implement the Parcelable interface, and the static CREATOR method. This case is for the lists

 private static final String MODEL_LIST = "MODEL_LIST";
    public MainFragment() {}
    
    public static MainFragment newInstance(ArrayList<YourModel>   
models) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(MODEL_LIST,models);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            ArrayList<YourModel> models = getArguments().getParcelableArrayList(MODEL_LIST);
        }
    }

Solution 5 - Android

First create Parcelable using Given Technique then

public static CreditCardDetail newInstance(CreditCardItemBO creditCardItem) {
        CreditCardDetail fragment = new CreditCardDetail();
        Bundle args = new Bundle();
        args.putParcelable(CREDIT_KEY,creditCardItem);
        fragment.setArguments(args);
        return fragment;
    }

And getting it like

 if(getArguments() != null)
 {
    creditCardItem = getArguments().getParcelable(CREDIT_KEY);               
 }

>where

public static final String CREDIT_KEY = "creditKey";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyital9View Question on Stackoverflow
Solution 1 - AndroidyorkwView Answer on Stackoverflow
Solution 2 - AndroidMACView Answer on Stackoverflow
Solution 3 - AndroidSohail PathanView Answer on Stackoverflow
Solution 4 - AndroidchryView Answer on Stackoverflow
Solution 5 - AndroidZar E AhmerView Answer on Stackoverflow