我正在开发运行Android 7.1.1的Google Pixel。
以下是2项活动,PlayActivity和ScoreScreenActivity。流程应该看起来像Main>播放>游戏>得分> [后退按钮]>播放> [后退按钮]>主要。
在ScoreScreenActivity中按下后退按钮时,将调用onKeyPressed()方法。我们被重定向到PlayActivity。到现在为止还挺好。现在我们处于PlayActivity状态,当再次按下后退按钮时,我们会不正确地从名为ScoreScreenActivity的Activity重定向回GameActivity。我们应该将其发送给MainActivity。
另外,我在GameActivity和ScoreScreenActivity中的startActivity()后面有finish()。此外,当我们从ScoreScreenActivity转到PlayActivity时会显示此错误:
W/ViewRootImpl[ScoreScreenActivity]: Cancelling event due to no window focus: KeyEvent
以下是活动代码: 不起作用的代码:
public class PlayActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Button mOneMinute = (Button) findViewById(R.id.play_one_minute);
if (mOneMinute!=null){
mOneMinute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(PlayActivity.this, GameActivity.class);
startActivity(intent);
finish();
}
});
}
}
@Override
public void onBackPressed(){
System.out.println("this isn't printed either");
// AlertDialog happens here
}
// Copy & Pasted from another StackOverFlow (http://stackoverflow.com/questions/16646301/onbackpressed-is-not-being-called)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("never gets printed");
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d("back", "should go back to MainActivity");
Intent i=new Intent(PlayActivity.this, MainActivity.class);
startActivity(i);
finish();
}
return true;
}
功能代码:
public class ScoreScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_screen);
int score = 0;
Intent intent = getIntent();
score = intent.getIntExtra("mScore", score);
System.out.println(score);
TextView mTV = (TextView) findViewById(R.id.score_text);
if (mTV != null ){
mTV.setTextSize(20);
mTV.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
String str = "Well Done!\nYour Score: "+ score +"\nThere's no high score system for now,\n" +
"so remember this score\nif it's a new high score!";
SpannableString ss = new SpannableString(str);
ss.setSpan(new RelativeSizeSpan(2), 0, 25, 0);
mTV.setText(ss);
}
Button mReturn = (Button) findViewById(R.id.score_return);
if (mReturn!= null){
mReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnIntent = new Intent(ScoreScreenActivity.this, MainActivity.class);
startActivity(returnIntent);
}
});
}
}
@Override
public void onBackPressed(){
System.out.println("this does not get printed");
// AlertDialog happens here
}
// Copy & Pasted from another StackOverFlow (http://stackoverflow.com/questions/16646301/onbackpressed-is-not-being-called)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("this gets printed");
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("back",""+1);
Intent i=new Intent(ScoreScreenActivity.this,PlayActivity.class);
startActivity(i);
finish();
}
return true;
}
}
答案 0 :(得分:0)
您可以这样处理:
适用于API级别5及更高版本的(新)
@Override
public void onBackPressed() {
// your code.
}
早于API 5
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event); // u are missing this
}