mardi 25 août 2015

Return to Last Opened Activity with Some Condition

i have a few interesting question here about how to return to latest activity if the game was restart 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?

Thank you in advance, have a good day




Aucun commentaire:

Enregistrer un commentaire