简体   繁体   English

从游戏结束画面返回主要活动

[英]Returning back to main activity from gameover screen

Hi so even though I have seen a couple of topics about linking activities or returning I can not get my return back to activity to work. 嗨,即使我看到了几个有关链接活动或返回活动的话题,也无法回到活动上班。 I abit of back ground the the app when the user gets an answer wrong or runs out of time it goes to game over taking the score across with it and displaying it on the game over, here's the working code for that if it helps solve my issue: Main Class: 当用户回答错误或用完游戏时,我会稍微退缩应用程序,因为它会导致分数超过分数并将其显示在游戏中,如果这有助于解决我的问题,则可以使用以下工作代码问题:主类:

 public void fail(){
        {
            Intent myIntent = new Intent(MainActivity.this,gameover.class);
            myIntent.putExtra("number", score);


            MainActivity.this.startActivity(myIntent);
            finish();

        }

gameover class: 游戏结束类:

  public void onCreate(Bundle savedInstanceState) {         
         super.onCreate(savedInstanceState);    
setContentView(R.layout.gameover);
re_run = (Button) findViewById(R.id.retry);
        EndScore = (TextView) findViewById(R.id.show);
        int getVal;
        getVal = getIntent().getExtras().getInt("number");
        String s = String.valueOf( getVal );
        EndScore.setText(s);     
  }

Now the reason I shared the above working code because I have the feeling the intent that takes the user and score to the gameover screen, is messing with the retry/return code as shown below: 现在,我之所以共享上面的工作代码,是因为我有一种吸引用户并在游戏结束屏幕上得分的意图,正困扰着重试/返回代码,如下所示:

private void setButtonOnClickListeners(){
          re_run.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent retry = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(retry);

            }
        });
}

From what I can find from those topics these seems to be the correct method. 从这些主题中我可以发现,这些似乎是正确的方法。 but when the code is run the re_run button does nothing. 但是在运行代码时,re_run按钮不起作用。 Any help? 有什么帮助吗?

You use " setButtonOnClickListeners() " to implement setOnClickListener and so override the onClick. 您可以使用“ setButtonOnClickListeners() ”来实现setOnClickListener,从而覆盖onClick。 But when did you call setButtonOnClickListeners? 但是什么时候调用setButtonOnClickListeners呢?

 public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.gameover);    re_run = (Button) 
    findViewById(R.id.retry);    
    EndScore = (TextView) findViewById(R.id.show);    
    int getVal;    
    getVal = getIntent().getExtras().getInt("number");   
    String s = String.valueOf( getVal );    
    EndScore.setText(s);    
    setButtonOnClickListeners();
    }

You can maybe check that your OnClick is working correctly adding a log line in LogCat. 您也许可以检查您的OnClick是否正常工作,并在LogCat中添加日志行。 Log.d("GameOver", "Retry onclick ok");

You are finding a view before it was inflated (R.id.retry). 您正在寻找一个未膨胀的视图(R.id.retry)。

Change the sequence of commands to: 将命令顺序更改为:

super.onCreate(savedInstanceState);    
setContentView(R.layout.gameover);
re_run = (Button) findViewById(R.id.retry);

Also try changing the intent to: 还可以尝试将意图更改为:

Intent retry = new Intent(GameOver.this, MainActivity.class);
retry.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

I personally used startActivityForResult(Intent) to start the game over screen, and once the player inputted his name or pressed something or quit the activity with Back, then it returned a value to onActivityResult(..) in which I called finish() . 我个人使用startActivityForResult(Intent)在屏幕上启动游戏,并且一旦玩家输入了自己的名字或按下了某个东西或使用Back退出了活动,然后它向onActivityResult(..)返回了一个值,我在其中调用了finish()

EDIT: 编辑:

Game ends, so I started activity using 游戏结束,所以我开始使用

            Intent i = new Intent();
            i.putExtra("Score", scoreCount);
            i.setClass(context, HighScoreInputActivity.class);
            context.startActivityForResult(i, 0);

Then when you insert the scores and stuff, you close the game screen in GameActivity with 然后,当您插入分数和内容时,您可以使用以下命令关闭GameActivity的游戏屏幕

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    this.finish();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM