When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}
Related
I'd like the back button to have behaviour similar to e.g.
// if I have an Item count > 1 and I'm not on ItemsListActivity
// back button takes me to ItemsListActivity
// else
// back button behaves like normal
Is this possible? If so, what do I need to do?
Yes.
Override onBackPressed()
(http://developer.android.com/reference/android/app/Activity.html#onBackPressed())
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
So you could do something like:
#Override
public void onBackPressed(){
if (count > 1) && (!(this instanceof ItemsListActivity)) {
// Launch ItemsListActivity / do whatever you want
}
else {
super.onBackPressed(); // Do the normal back press functionality
}
}
There is probably a better way to check what activity you are in!
Is it possible: you have to override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the back button
if ((keyCode == KeyEvent.KEYCODE_BACK) && itemCount > 1) {
// Back button takes you to ItemsListActivity
return true;
}
// Otherwise execute requested action
return super.onKeyDown(keyCode, event);
}
By the way, when user hit back, normally they expect to return on previous activity or exit app. IMHO it is better to make another way (button or menu item) to access on your ItemsListActivity.
I have a basic question about android development. In my app i have a autocomplete box. When user click on a menu button, autocomplete become visible. When user clicks on an entry in autocomplete, the text views show it. I want to use back button such that when user presses it . Textview become invisible , on again click autocomplte disappears and than on third click app exits. But in my app on a single click app exits. I am a new to development so doesn't know much about it, so i ask here.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//stuff
}
return super.onKeyDown(keyCode, event);
}
Since API 5 :
#Override
public void onBackPressed() {
//stuff
}
How I can change working of home button in android? I want to when I click on home button I do some actions and after that application should go to background. How I can do that.
Write your own home screen. When the user presses HOME, they will get a choice of running your app or the built-in home screen. They can elect to choose one just this one time, or set either app as being their default from now on. Many will wonder why on Earth you decided to decided to implement a home screen.
Most developers care about any case where their activity moves to the background, in which case you can either use a lifecycle method (e.g., onPause(), onStop()) or try onUserLeaveHint().
I do it this way:
/* Handles item selections */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//You can do whatever you want here
Intent homeInt = new Intent(this, SomeActivity.class);
startActivity(homeInt);
return true;
}
return false;
}
Have you tried overriding onKeyDown()
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// ENTER CODE HERE
return true;
}
return super.onKeyDown(keyCode, event);
}
I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity
How do I have an app return to the 'Home' screen when the user is on a different class and they click the back/return button on their phone?
Right now, when they access a different class and hit the back button, it goes back to the Home screen on the Droid rather than the Home screen on the app - how do I get it to go back to the Home Screen on the app?
Thanks!
Use OnBackPressed() method of Activity class.
Make conditions like :
public void onBackPressed(){
if(conditon)
{
detailInfoList.setVisibility(View.VISIBLE);//or anything that you want to show
}
}
you can't,
the home button is the only you can't prevent form closing your app. Its done so that you can't block the user inside your applications (like a virus).
the other keys like back menu and research are possible
in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(keyCode == 82){
Log.i("jason","released on menu");
//change view
//return so that the other behavior (leaving the activity) is not tirggered
return false;
}
}
Log.i("jason","actual key code "+keyCode);
return super.onKeyDown(keyCode, event);
}