Hey guys I have this activity flow on my game. Start page>List of stages(from stages 1-10)>questions. If the user press the start button on the start page he will go to the list of stages, from there the only button that is enabled is stage 1 then the rest are in disable mode. In stage 1 there are 5 questions and after the user finish answering them correctly, the user will be directed back to the list of stages in which stage 2 is already unlocked as well as stage 1(stage 1 is enabled in default). Now what I want to happen is when the user exits the application using home button or back button then the user open the application again, after pressing the start button from start page he will be directed to the list of stages in which the stage 2 is still unlocked or any stages the user last finished.
Asked
Active
Viewed 206 times
0
-
Explain your problem in small, precise.. – Ajay S Jan 31 '13 at 14:56
-
1...and with some whitespace – Simon Jan 31 '13 at 14:57
-
sorry for that. Well the main question of these is How can I resume(I don't know if the term "Resume" is correct) the last activity that I finished? for example I already unlocked stage2 or whatever stage, If I closed my application and upon returning, stage 2 should still be unlocked or whatever stages the user unlocked. – Jerome Jan 31 '13 at 15:06
-
You want to open the last activity again when user start the app. right – Ajay S Jan 31 '13 at 15:10
-
@TGMCians yes that's exactly what I want to happen. if the user unlocked stage 2 and decided to quit the game, by the time he relaunch it again, after pressing start button, the stage 2 should be unlocked – Jerome Feb 05 '13 at 12:00
2 Answers
1
As I don't understand fully your questions I have two propositions:
- Save stage info in onPause or onStop and then retrieve it in onCreate method (http://developer.android.com/guide/components/activities.html#Lifecycle), or
- Save everything in onStop in SharedPreferneces and retrieve when needed. I suppose this option is better.
sebap123
- 2,371
- 6
- 40
- 76
0
To return to the last open activity When relaunched the app.
To do this please.
Source: How to make an android app return to the last open activity when relaunched?
@Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
public class Dispatcher extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class<?> activityClass;
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(
prefs.getString("lastActivity", Activity1.class.getName()));
} catch(ClassNotFoundException ex) {
activityClass = Activity1.class;
}
startActivity(new Intent(this, activityClass));
}
}
-
in my case the flow is when you open my app, you will first see the start page which has a start button on it, then when you press start, you will see the list of stages from stage 1 to 10 but here stage 1 is the only button that is enabled. When you click stage 1 there will be 5 questions that you need to answer in order to unlock stage 2. now lets say that I already unlocked stage 2, If I closed the game and open it again, when I press the start button I will be directed to the list of stages in which 2 buttons should be enabled (stage 1 and stage 2) – Jerome Jan 31 '13 at 15:23
-
should I put the code on the class I want to save? for example I have a stage2.class. – Jerome Feb 05 '13 at 13:24
-
-
from the last question on my stage1 if it is answered correctly it will intent to the list of stages class that stage 2 button is already enabled. So the code `@Override protected void onStop() { super.onStop(); SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE); Editor editor = prefs.edit(); editor.putString("lastActivity", getClass().getName()); editor.commit(); }` will be put in my stages.class? – Jerome Feb 05 '13 at 14:04
-
@Jerome I think you should write onStop method in each class. When user re open the app again it will open the activity where user was at last. – Ajay S Feb 05 '13 at 15:05
-
what I want to happen is whenever a user unlocked a stage it will be saved so that if he quits the game, the last session or stage he achieved will still be saved. and when he opens the game again, the last stage he unlocked will still be unlocked – Jerome Feb 05 '13 at 15:07
-
but my app has a start button that will intent to the list of stages in which stage 1 is the only button that is enabled and button stage 2 to 10 in disabled. if the user unlocked stage 2, it should be saved so that if the user quits the game and start it again, when he press the start button he should be directed to the list of stages in which stage 2 is already unlocked or what ever stage the user unlocked. – Jerome Feb 05 '13 at 15:36
-
-
so how can I locate you on fb? is there any way here to message you privately? – Jerome Feb 05 '13 at 16:17
-
should I create a class named Dispatcher on my project and from there I will put the codes? – Jerome Feb 11 '13 at 18:23