1

Let's say I have four activities: StartActivity, ConfigureActivity, GameActivity, and ResultActivity. The flow of activities is as such:

  • StartActivity starts ConfigureActivity with intent
  • ConfigureActivity starts GameActivity with intent
  • GameActivity can go back directly to StartActivity by starting it with intent
  • GameActivity can start ResultActivity with intent
  • ResultActivity starts StartActivity with intent

While in GameActivity, the user has the opportunity to gather Strings that he or she want to save for later use. These are added to a simple ArrayList of Strings which I will later need in StartActivity.

From GameActivity, the user might either start the ResultActivity, or exit the current activity to go back to StartActivity.

When going from GameActivity to ResultActivity, and then from there back to StartActivity, the ArrayList is passed along the way through intents. On regular exit, which is done by listening to onKeyDown, an intent is in the same way (data is passed back to StartActivity).

Now, I know you can use startActivityForResult to start an activity with excpectation to get something back. But I since my StartActivity actually launches ConfigureActivity, and that ConfigureActivity will not at all be involved with this data, how can I do this? I use SharedPreferences for application settings, but there I have xml-files with items that will never change. In this case, the list might vary from 0 to maybe 100 strings. Should I use sqllite?

Also, I am not providing any code because I am only looking for some conceptual guidance and I do not specifially need code example as response to this question.

Wu Wenjun
  • 107
  • 2
  • 11
  • 1) Use sharedpreferences 2) show some code – statosdotcom Jun 02 '18 at 00:52
  • 1
    How many elements do you expect to be in this `ArrayList`? Will there be multiple lists? – Barns Jun 02 '18 at 01:21
  • `But I since my StartActivity actually launches ConfigureActivity`. Pretty late you are telling this. You should have started with it. So ConfigureActivity starts GameActivity? – greenapps Jun 02 '18 at 06:54
  • @statosdotcom just after posting this question I thought about SharedPreferences, as I use it for.. well application settings. However, for that I use xml files with items to populate e.g. ListPreference objects. I don't know if I can use that when the list that is created in GameActivity will be of dynamic size. I might aswell be using some kind of Sqllite database I assume. 2) I should have made it more clear in my question that I am looking more for some conceptual guidance rather than code examples. Hence, I did not give any code in the question. Thank you for your feedback! I will clarify – Wu Wenjun Jun 02 '18 at 08:49
  • @Barns there might be around 100, so not much. Only one list. – Wu Wenjun Jun 02 '18 at 08:50
  • @greenapps you are right, I should have mentioned that earlier. I struggeled with trying to explain my problem conceptually. I will clarify. – Wu Wenjun Jun 02 '18 at 08:50
  • Take a look at the accepted answer to this question and let us know if that would solve your problem :: https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Barns Jun 02 '18 at 15:57
  • @WuWenjun thank you for your reply. You are on the right track. Maybe sqlite would be a great choice too, it's very easy to deal, runs speedy and is powerful too. Best luck. – statosdotcom Jun 03 '18 at 00:41

1 Answers1

1

Only the StartActivity has to create a new Intent.

All the other activities use the same intent.

At forwarding while starting a new activity but also used as result intent for setResult().

Different activities just use intent.putExtra() to put extra data.

In this way all putExtras() keep remained and every activity can read the putExtras of all other activities.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Don't I need the intent object in subsequent activities in order to call the putExtra? – Wu Wenjun Jun 02 '18 at 10:16
  • 1
    Yes of course. You get it with Intent intent = getIntent(). You change the action to the next activity. Put some extras in it and then start the intent. – greenapps Jun 02 '18 at 12:18