Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press back button to another activity

Tags:

java

android

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?

like image 896
brian Avatar asked Nov 29 '22 17:11

brian


1 Answers

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);
}
like image 51
Rakshi Avatar answered Dec 05 '22 05:12

Rakshi