Handle back key in webview in android
Getting input of mobile hardware back key in android
For handle back key we have to create onKeyDown method like this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
// here codes for back key function
return true;
}
return super.onKeyDown(keyCode, event);
}
Function should return ture value if back key is pressed. else it will return super.
For KeyEvent import header file
import android.view.KeyEvent;
For handle back key in adroid 2.0 we have onBackPressed method
@Override
public void onBackPressed(){
// here codes for back key function
return;
}
Example :-
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
WebView ourSite = (WebView) findViewById(R.id.webView1);
if(ourSite.canGoBack())
ourSite.goBack();
else
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}//end of onKeyDown
//for android 2.0
@Override
public void onBackPressed(){
WebView ourSite = (WebView) findViewById(R.id.webView1);
if(ourSite.canGoBack())
ourSite.goBack();
else
finish();
return;
}// end of onBackpressed
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}//end of onCreateOptionsMenu
}//end of MainActivity
Getting input of mobile hardware back key in android
For handle back key we have to create onKeyDown method like this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
// here codes for back key function
return true;
}
return super.onKeyDown(keyCode, event);
}
Function should return ture value if back key is pressed. else it will return super.
For KeyEvent import header file
import android.view.KeyEvent;
For handle back key in adroid 2.0 we have onBackPressed method
@Override
public void onBackPressed(){
// here codes for back key function
return;
}
Example :-
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
WebView ourSite = (WebView) findViewById(R.id.webView1);
if(ourSite.canGoBack())
ourSite.goBack();
else
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}//end of onKeyDown
//for android 2.0
@Override
public void onBackPressed(){
WebView ourSite = (WebView) findViewById(R.id.webView1);
if(ourSite.canGoBack())
ourSite.goBack();
else
finish();
return;
}// end of onBackpressed
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}//end of onCreateOptionsMenu
}//end of MainActivity
Leave reply
Add your comments here