How to pass an object (instance of a class) from Mainactivity to another activity (Mainactivity2) on Androidstudio

Asked

Viewed 25 times

1

Superherofeatures is a class with all the attributes of a superhero, such as name, birthday, power and many other things. Now the heroSelected instance is working, but I can’t transmit it to the other activity. Does anyone know how to do this?

In Mainactivtity, I have a function that calls:

    SuperHeroFeatures heroSelected = superHeroFeaturesList.get(position);
    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
    intent.putExtra("superHeroFeatures", heroSelected);
    startActivity(intent);

In Mainactivtity2, I have a function that calls:

public class MainActivity2 extends AppCompatActivity {
    Intent intent = getIntent();
    SuperHeroFeatures superHeroFeatures = (SuperHeroFeatures) intent.getSerializableExtra("superHeroFeatures");
    Context context;
    TextView name;
    ImageView imag;

    public MainActivity2(Context context, SuperHeroFeatures superHeroFeatures) {

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hero_features);
        name= findViewById(R.id.responseView);
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.