I try the code below to call another activity while pressing the back button:
@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
switch(keyCode) {
case(KeyEvent.KEYCODE_BACK):
Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window w = NASGroup.group.getLocalActivityManager().startActivity("BActivity", intent);
View view = w.getDecorView();
MyGroup.group.setContentView(view);
return true;
}
return false;
}
But when I press the back button, it get out of the app.
I see the logcat, it does not run the function onKeyUp
and doesn't output any message.
The same code in onKeyUp
, I try to below code to a button in layout and it works.
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window w = NASGroup.group.getLocalActivityManager().startActivity("BActivity", intent);
View view = w.getDecorView();
MyGroup.group.setContentView(view);
}
});
How can I modify it?
To handle back press you have to override Onbackpress method.
@Override
public void onBackPressed() {
finish();
Intent intent = new Intent(Myactivity.this, other.class);
startActivity(intent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With