I have a few interesting question here about how to return to latest activity if the game was restart/re-open because I have a new game button and continue button.So when continue button clicked it will return to last activity that opened before and the condition is activity is random from activityone to activityfive
I will explain with my code
this is menu.class
public class menu extends Activity {
int level;
Button newgame, continues, continuelocked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
continuelocked=(Button)findViewById(R.id.buttoncontinuelocked);
continues=(Button)findViewById(R.id.buttoncontinue);
newgame=(Button)findViewById(R.id.buttonnewgame);
newgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent i =new Intent(menu.this, intro.class);
startActivity(i);
}
});
}
public void onResume() {
super.onResume();
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
level = pref.getInt("Level", 0);
if(level == 0)
{
continuelocked.setVisibility(View.VISIBLE);
continues.setVisibility(View.GONE);
}
if(level == 1)
{
continuelocked.setVisibility(View.GONE);
continues.setVisibility(View.VISIBLE);
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", level);
editor.commit();
continues=(Button)findViewById(R.id.buttoncontinue);
continues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
//How to set this method to return to latest activity that i play before
//if i use random levelactivity?
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and in intro.class I do this method to make activity random, check my code below here -
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
and in every Activity "One to Five" I put the same random activity code
@Override
public void onClick(View v) {
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
}
So My question is
First. How to open the last Activity with a Continue button if Activity was Random?
Second. if in every Activity had a same Random code to One until Five, How to set Disabled to Activity that already Opened before?
Anyone can explain about this?
UPDATED
I have found a solution with my second answer, but i dont try it yet so i dont know it work or not
so i changed the code like this
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
layout7.setVisibility(View.GONE);
layout7.setVisibility(View.VISIBLE);
// Here, we are generating a random number
Random generator = new Random();
number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
// E.g., if the output is 1, the activity we will open is ActivityOne.class
case 1: if(one == 1){
activity = ActivityOne.class;
}
else if(one == 2){
Random generatorone = new Random();
number = generatorone.nextInt(5) + 1;
}
break;
case 2: if(two== 1){
activity = ActivityTwo.class;
}
else if(two== 2){
Random generatortwo = new Random();
number = generatortwo.nextInt(5) + 1;
}
break;
case 3:if(three== 1){
activity = ActivityThree.class;
}
else if(three== 2){
Random generatorthree = new Random();
number = generatorthree.nextInt(5) + 1;
}
break;
case 4:if(four == 1){
activity = ActivityFour.class;
}
else if(four == 2){
Random generatorFour = new Random();
number = generatorFour.nextInt(5) + 1;
}
break;
default:if(five== 1){
activity = ActivityFive.class;
}
else if(five== 2){
Random generatorfive = new Random();
number = generatorfive.nextInt(5) + 1;
}
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
};
i think, if the int was show ==2 its mean the Activity was already opened before. so it will random again till found activity with ==1
can anyone correct my code above? it is right or not?
and my first question still dont have an answer
First. How to open the last Activity with a Continue button if Activity was Random and the app was re-open/restart?
Thank you in advance, have a good day