Share object of a class Parcelable between Activities on Android

1

Some practical example of how it would be to share a class by implementing Parcelable to share an object between Activities?

The class I have already defined

public class Interstitial extends Banner implements Parcelable {
...

The object that I want to share between activities

    Interstitial entryAd.setType(Interstitial.TypeInterstitial.APPGAME);
    entryAd.setTitle1("Your internet-100X Faster");
    ...

I still need to arm the Intent and receive the intent to recover the object in the other activity.

    
asked by Webserveis 14.12.2016 в 19:38
source

2 answers

0

The intent can be assembled in the following way:

For shipping:

Intent i = new Intent();
i.putExtra("nombre_extra", objectoParcelable);

You receive:

  Intent i = getIntent();
  ObjectoParcelable objectoParcelable = (ObjectoParcelable) i.getParcelableExtra("nombre_extra");

Extra

This link takes you to a generator of Parcel objects

link

Greetings.

    
answered by 14.12.2016 / 19:45
source
2

Send objects between Activities:

One option is to implement the class Serializable on your object:

public class Interstitial implements Serializable {

You would send an ArrayList of objects in Intent using .putExtra() :

   Intent intent = new Intent(MainActivity.this, SegundaActivity.class);
                intent.putExtra("listaInterstitial", listaInterstitial);
                startActivity(intent);

To receive the ArrayList of objects in the Activity, it is done in the following way:

ArrayList<listaInterstitial> listaInterstitial = (ArrayList<listaInterstitial> ) getIntent().getSerializableExtra("listaInterstitial");
    
answered by 14.12.2016 в 20:21