2015-08-26 25 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

相关问题