-3

So, for my Android course this semester we're making an app of our own choosing, and due to a lack of ideas I decided to go with a text-adventure styled game. Nothing too complicated, I know. I'm making sure to be creative and incorporate a lot of different functions of the phone, such as the accelerometer and the camera. But I digress.

I've started planning out how the app should work, and how I should go about coding it, but I've come upon an obstacle fairly early that I need to find a solution for. I'm planning on creating it so that the player is sent from activity to activity, which I think is the best way to go about it, unless I do an endlessly scrolling activity that fills out as the player progresses. And thus, I need a way to make it so that when the player closes the app completely it will "continue" on the last activity before shutdown, so the progress is saved in a way.

I'm not sure if this is possible to do, and if so, are there any other ways I can achieve the same sort of result?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PQuix
  • 83
  • 2
  • 10
  • You can use `SharedPreferences` to save the user state and retrieve it when needed. It is your job to implement the logic to go to the correct screen/activity based on this state. – Benjamin Oct 02 '17 at 11:08
  • When you say "save the user state" do you then mean it saves the screen/activity the user is standing at when the action occurs? – PQuix Oct 02 '17 at 12:12
  • It's up to you how to model your user state. For example, for a book reader app, you could save the last page the user was reading when he left the app and resume at the same page when he comes back. – Benjamin Oct 02 '17 at 12:16

2 Answers2

0

There is no way that you can change your starting activity programmatically. You can save the activity that you want to start with in a file and redirect to this activity from your main activity every time your application starts (on the onCreate() method of your main activity).

As it says here.

  • Okay, so basically, it's impossible to do what I was thinking of, because the activity I would like to start with would change every time the player started up the app. – PQuix Oct 02 '17 at 11:34
  • You could update the file with the current activity every time the user progresses in your game. – P. Frangidis Oct 02 '17 at 11:40
0

As someone has suggested, save the current state (ie: what Activity is currently shown) in SharedPreferences. When the user launches your app, the root Activity (the one with ACTION=MAIN and CATEGORY=LAUNCHER) will be started. In onCreate() of the root Activity, read the saved state from the SharedPreferences and launch that Activity immediately.

David Wasser
  • 93,459
  • 16
  • 209
  • 274