views:

35

answers:

1

Lets say we have a main class with 5 buttons, where when each clicked go to an activity that displays information with 2 textviews.

The layout for all 5 activities will be the same, so naturally I would want to use one activity and reuse it by changing the text displayed in those 2 textviews for each button pressed.

How can I do this? Thinking about having a global intent in my main class, which is called by startActivity() for each button together with .putExtra() method to send the extra data in order to know from what button it came from (so that I can change the textviews).

Any other solutions that are better?

One more question, how does one change the title of the activity with java code while the app is running? Before the app starts one can use the xml, but how to change it if I'm trying to reuse the activity?

<activity android:name=".MynewClass"
          android:label="@string/class_text">
</activity>
+2  A: 

The layout for all 5 activities will be the same, so naturally I would want to use one activity and reuse it by changing the text displayed in those 2 textviews for each button pressed.

That is certainly one possibility.

How can I do this?

Your solution (Intent with extras) seems fine. However, I would not make it a "global" Intent, since you are changing it via the extras. Just create a new Intent when you need it.

how does one change the title of the activity with java code while the app is running?

Call setTitle().

CommonsWare
Just one more question. Won't creating new intents all the time with new intent(this,mysecondclass.class) from the main activity actually create a new activity? Or are intents still calling only one activity during all times?
Milan
No, creating new `Intents` has no bearing on whether Android creates new instances of the `Activity`. By default, new instances of the `Activity` are created. See `android:launchMode` (in Activity manifest) or `FLAG_ACTIVITY_SINGLE_TOP` and `FLAG_ACTIVITY_CLEAR_TOP` (to control this behavior on a per-`Intent` basis).
CommonsWare