我正在開發運行Android 7.1.1的Google Pixel。Android 7.1.1後退按鈕方法不被稱爲
以下是2項活動,PlayActivity和ScoreScreenActivity。流程看起來應該像Main> Play> Game> Score> [back button]> Play> [back button]> Main。
在ScoreScreenActivity中按下後退按鈕時,會調用onKeyPressed()方法。我們被重定向到PlayActivity。到現在爲止還挺好。現在我們處於PlayActivity中,當再次按下後退按鈕時,我們將不正確地從名爲ScoreScreenActivity的Activity重定向回GameActivity。我們應該將其發送給MainActivity。
另外,我在GameActivity的startActivity()和ScoreScreenActivity中完成了()。
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;
}
}
發表您的活動Ç –
@Charuka我貼我的代碼樣本,而對於一些我的代碼現在起作用了。但不知道爲什麼它不提前工作。 – joshua
任何不能按預期工作的東西現在? –