webview activity in android - android

I have two activity let A and B. A is listActivity and B is webViewActivity. Now in webview, when i back pressed then it works fine within webview but in last back pressed, it resides in activity B but I want to go back to Activity A (listActivity).
My code for back pressed
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks.

This may help you...
http://rickluna.com/wp/2014/04/disable-android-back-button-in-inappbrowser/
here it explains that you can disable back button in webview... so make this works in your logic.

#Override
public void onBackPressed() {
super.onBackPressed();
}
onbackpressed function call when you pressed back button then finish current activity.

Related

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Back button stuck in endless loop?

My app is currently stuck in an endless loop with the physical back button. The program is set up to load a splash screen and then transition to a main menu. Once at the main menu the user can switch to another activity of their choice. For example: The new game activity. Once the user is at the new game activity I want them to be able to hit the back button and have it take them to the main menu. Once back at the main menu, if they hit the back button again I would like it to exit the game.
This is what I am using for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
}
return super.onKeyDown(keyCode, event);
}
It works properly and takes the user back to the main menu without a problem. But if the user hits the back button again once at the main menu it will take them to the screen they just escaped from. So it just loops back to the previous screen every time.
This is how the main menu is set up:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
If I press the back button as soon as the main menu loads the first time it will work properly and close the game. It only messes up if I've hit the back button previously to get from one screen to the main menu.
Update:
Okay that kind of worked. When I hit the back button at the main screen the music stops and it acts like it's trying to close the app but it flashes back to the main screen again. I have to hit it twice every time.
You should call finish() inside the onKeyDown method of your activities so your current activity is actually removed from the stack.
Something like this should work:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
finish();
}
return super.onKeyDown(keyCode, event);
}
First (from comments) add return true; within the if statements (to indicate that you have handled the event), like so:
// Main menu code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
return true; // Add me!
}
return super.onKeyDown(keyCode, event);
}
Additionally, I believe because you're "starting" the menu activity again, you have to clear the back stack (the order of activities used so far) before opening it. You should be able to achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or maybe Intent.FLAG_ACTIVITY_CLEAR_TASK). Something like this:
// Other activity code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me!
startActivity(menuIntent);
return true; // Add me, too!
}
return super.onKeyDown(keyCode, event);
}
You haven't called finish() in your other activities so when you call finish() on the menu it just closes itself and the other activity is exposed

How to catch event with hardware back button on android?

How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the method onBackPressed() in whatever Activity you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
You can suppress user "back" action with onKeyDown() in your activity like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//do actions like show message
return false;
}
return super.onKeyDown(keyCode, event);
}

Android Home button click ignoring saved state?

Can I ignore the previous stage of application after tapping Home button in android?
So that I can get the application from initial screen.
Use like this.If now you are in LastActivity and you want to go back to the First Activity and there is more activity in between then if you want to go first from last use
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(autoSearch)
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.d("click","HOMEButtonclicked");
LastActivity.this.startActivity(new Intent(LastActivity.this, FirstActivity.class));
moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}

Android webview back button

In my app when user clicks on a gallery widget i open the image in a webview as it supports zoom functionality.
Now when user clicks back button on webview i want the user to see my application. But it goes directly to home screen.
How should i handle the onBackKeyPressed() ?
Override the Activity's onKeyDown(..) event
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// close image gallery
return false; // this avoids passing to super
}
return super.onKeyDown(keyCode, event);
}
*edit: code
#Override
public onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();

Categories

Resources