2015-08-26 43 views
1

我有一些有趣的問題,關於如果遊戲重新啓動/重新打開時如何返回到最新活動,因爲我有一個新遊戲按鈕並繼續按鈕當。所以繼續按鈕,點擊它會返回到之前打開的條件是活動是從activityone隨意activityfive返回上次打開的活動,如果應用程序在某些情況下重新開啓

我將與我的代碼解釋最後一項活動

這是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 intro.cla SS我這樣做的方法,使活動隨機, 檢查下面在這裏我的代碼 -

@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); 
    } 

,並在每個活動「一五」,我把相同的隨機活動代碼

@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); 
} 
} 

所以我的問題是

首先。如果活動是隨機的,如何使用繼續按鈕打開最後一個活動?

第二。如果在每個Activity中有一個相同的隨機代碼,直到Five,如何將Disabled設置爲之前已打開的Activity?

任何人都可以解釋一下這個嗎?

修訂

我發現與我的第二個答案的解決方案,但我不試試又那麼我不知道它的工作或沒有

,所以我改變了這樣的代碼

@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); 
    } 
}; 

我認爲,如果int是show == 2其意思是Activity已經打開過。所以它會再次隨機,直到找到活動與== 1

任何人都可以糾正我的代碼上面?它是對的還是不對?

,我的第一個問題仍然沒有有一個答案

第一。如果Activity是隨機的並且應用程序已重新打開/重新啓動,如何使用繼續按鈕打開最後一個活動?

謝謝你在前進,有一個美好的一天

回答

1

關於第一個問題,一旦你打開你的隨機Activity可以使用SharedPreferences存儲的ID爲Activity,一旦你按你繼續按鈕,您可以閱讀此首選項,獲取此ID並打開相應的Activity

例如,創建一個實用程序類來處理SharedPreferences

public class ActivityManager { 

    private static ActivityManager instance = null; 

    private SharedPreferences sharedPref; 

    private Context context_; 

    private final String prefName = "preferencesHandler"; 

    public static ActivityManager getInstance(Context context) { 
     if (instance == null) { 
      instance = new ActivityManager(context); 
     } 

     return instance; 
    } 

    private ActivityManager(Context context) { 
     context_ = context; 
     sharedPref = context_.getSharedPreferences(prefName,Context.MODE_PRIVATE); 
    } 

    public void saveActivity(int ID) { 
     editor = sharedPref.edit(); 
     // Key,Value 
     editor.putInt("activity_id",ID); 

     editor.commit(); 
    } 

    public int getActivityID() { 
     // Key, Default Value 
     return sharedPref.getInt("activity_id",0); 
    } 
} 

一旦你打開隨機Activity中的每一個,設置一些ID給他們,並使用上述類保存。然後在需要時,用getActivityID()檢索該值。這應該夠了吧。

有關如何使用SharedPreferences的詳細信息,請參閱this link


編輯:您的具體情況,有你這部分代碼:

// 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; 
} 

您可以輕鬆地使用number變量作爲ID。所以,一旦您隨機獲得該號碼,您也可以使用SharedPreferences進行保存。當您按繼續Button時,只需讀取該值並再次使用switch-case即可獲得正確的Activity

+0

哦好吧我知道了,但我仍然奇怪與集ID爲活動你可以給這個身份證Mauker的例子? – RichFounders

+0

完成。試試那個ideia @Chairizky – Mauker

+0

所以這個「數字」我應該使用一個靜態的int還是隻給int它Mauker? – RichFounders

相關問題