Search code examples
androidandroid-tabhostandroid-tabactivity

onBackPressed method and onKeyDown is not working properly


I am using this code to open new activity

View view = getLocalActivityManager().startActivity("recently_viewd", 
                        new Intent(context,Job_Description.class)
                  .putExtra("line", result)
                    .putExtra("limit",0)
                    .putExtra("Alert", false)
                    .putExtra("str_Descrption",edit_Jobdesc.getText().toString().trim())
                    .putExtra("str_location", edit_JobLoc.getText().toString().trim()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                        .getDecorView();

                setContentView(view);

And in a Job_Description.class i want to handle android back key. I override OnBackPressed() method on Job_Description.class but it is not working.

@Override
 public void onBackPressed() {
     super.onBackPressed();
     Toast.makeText(this, "BAck Click", Toast.LENGTH_LONG).show();
     finish();
        startActivity(new Intent(context, Tab_Bar.class));
 return;
 }

after that i implement OnKeyDown() method but it's also not working.

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
               Toast.makeText(this, "BAck Click", Toast.LENGTH_LONG).show();
         finish();
            startActivity(new Intent(context, Tab_Bar.class));
         return true;
     }

     return super.onKeyDown(keyCode, event);
 }

Please help me how i can handle back button Thanks In Advance.


Solution

  • Use like this:

    @Override
     public void onBackPressed() {
    
         Toast.makeText(this, "BAck Click", Toast.LENGTH_LONG).show();
         finish();
            startActivity(new Intent(context, Tab_Bar.class));
    super.onBackPressed();
     return;
     }