MrAnderson1992 MrAnderson1992 - 5 months ago 10
Java Question

Open same activity with different result. From MainActivity

I am wondering if it is possible to open a new activity but with different results depending on which button has been pressed. For example:

Main Activity -
btnApple
btnBanana
btnGrape
btnOrange
...

Pressing any of these buttons would take you to "FruitInfoActivity" however, depending on the button pressed would give you a different text view of say a description of the fruit.

The alternative is to have a new activity for every single fruit, which as an example would be a lot of activities.

Any help would be great thanks.

Answer

You can pass information in the intent you create for starting the new activity.

Intent intent = new Intent(getBaseContext(), FruitInfoActivity.class);
intent.putExtra("FRUIT_NAME", "Apple");
startActivity(intent)

And you can get this information in new Activity (FruitInfoActivity) to show it in your text field.