Question

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.

Was it helpful?

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;
 }

OTHER TIPS

Just write:

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

It will work..

First of all, you should finish your current activity after starting the next one. The order should be other way around for these calls.

startActivity(new Intent(context, Tab_Bar.class));    
finish();

And also, since you're handling the back button, don't call super.onBackPressed(); at all. Because what it does is finishing the current activity, but you're already doing it.

Try this simple code. Change webView to your WebView var

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top