I'm developing on a Google Pixel running Android 7.1.1.
Below are 2 Activities, PlayActivity and ScoreScreenActivity. The flow should look like Main > Play > Game > Score > [back button] > Play > [back button] > Main.
When the back button is pressed in ScoreScreenActivity, the onKeyPressed() method is called. We get redirected to PlayActivity. So far so good. Now that we're in PlayActivity, when the back button is pressed again, we INCORRECTLY get redirected back to GameActivity, from the Activity which called ScoreScreenActivity. We should instead be sent to MainActivity.
Also, I have finish() after startActivity() in GameActivity, and in ScoreScreenActivity. And also, this error gets displayed when we go from ScoreScreenActivity to PlayActivity:
W/ViewRootImpl[ScoreScreenActivity]: Cancelling event due to no window focus: KeyEvent
Here are the activity codes: Not functional Code:
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;
}
Functional Code:
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;
}
}
You can handle it like this:
for API level 5 and greater(new)
older than API 5