java - 如果应用程序在某些情况下重新打开,则返回上次打开的 Activity

标签 java android android-activity random sharedpreferences

我有几个有趣的问题,关于如果游戏重新启动/重新打开如何返回到最新的 Activity ,因为我有一个新的游戏按钮和继续按钮。所以当继续按钮被点击时,它将返回到上一个打开的 Activity 之前,条件是 Activity 从 Activity 一到 Activity 五是随机的

我会用我的代码来解释

这是菜单类

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

在 intro.class 中,我使用此方法使 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();                     

     // 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 中,我都放置了相同的随机 Activity 代码

@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 是随机的,如何使用继续按钮打开最后一个 Activity ?

其次。如果在每个 Activity 中都有相同的随机代码到一到五,如何将 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

任何人都可以更正我上面的代码吗?对不对?

我的第一个问题还是没有答案

首先。如果 Activity 是随机的并且应用重新打开/重启,如何使用继续按钮打开最后一个 Activity ?

提前谢谢你,祝你有美好的一天

最佳答案

对于您的第一个问题,一旦您打开随机的 Activity,您就可以使用 SharedPreferences为该 Activity 存储一个 ID,一旦您按下继续按钮,您就可以读取此首选项,获取此 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 保存它。当您按下 continue Button 时,只需读取该值并再次使用 switch-case 即可获得正确的 Activity.

关于java - 如果应用程序在某些情况下重新打开,则返回上次打开的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227232/

相关文章:

java - 从 JSON Volley 请求中获取值的正确方法是什么?

android - 我必须在 Android Manifest 中指定父 Activity 名称吗?

android - 如何将任何对象从 Activity 传递到服务?

java - 寻找最长的名字

Java - 数组没有打印我想要的值

java - 如何在 Jetty Web 浏览器关闭时终止 javaw.exe

安卓 fragment : open context menu programmatically

android - 识别没有登录的用户

android - 从sys目录读取文件

android - 我是否必须创建一个 native Activity 来获取窗口,或者我可以从 Android 设备上的窗口管理器为 vulkan 应用程序提供一个?