How to make WebView inside fragment to go back to previous tab? - java

I have a fragment which contains a webview.And the fragment is reused in all the four pages of a viewPager.What i want is, when the user clicks the back button i want the webView Fragment to go back to the previous tab of the current webViewFragment.And finally exit the activity.I can easily do this if the webView was inside an activity like this:
#Override
public void onBackPressed() {
if (mWebview.canGoBack())
mWebview.goBack();
else
super.onBackPressed();
}
But i cannot do this in a fragment because the fragment cannot override onBackPressed().How can i do this inside a fragment which is inside a viewPager

You can implement setOnKeyListener on webview to handle back button.
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});
If there is no previous page in your webview then it will trigger usual onBackPressed.
Hope it helps you.

This code is perfect for fragment webview backpress.
You can implement setOnKeyListener on webview to handle back button
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});

Related

How to get hardkey events in a system overlay with a unfocusable view?

As I described in the Title, my problem is that there is a view that is not focusable. So now I've no idea how to get KeyEvents from the View...
Edit:
If possible, event can be listened from service
I have used this code in a fragment to close a full screen pager view and it works
View v = inflater.inflate(R.layout.fragment_addetails, container1,
false);
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& pager.getVisibility() == View.VISIBLE) {
pager.setVisibility(View.GONE);
return true;
}
return false;
}
});
or this
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//do code
return true
}
return false;
}
maybe this will help
if not please describe the full scenario for what your are trying to achieve
Ok I found out, that I can use the MediaButton to get the KeyEvent I wanted. I just needed to declare a BroadcastReceiver and register it. Thats it!

I need to minimize the android application on back button click

I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);

Executing simple code on android app before closing

Hello, I've been trying to integrate airpush in andriod but I've been having some issues.
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
As you can see is the andriod go back code for webview via history.
The problem is that I want execute this code when it has no more history, meaning when the application is about to close.
airpush.startLandingPageAd();
Any help will be greatly appreciated guys :)..
You need to edit your code, like this:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
else
{
//Your webview doesn't have any history here and you can close your app here.
//You can also execute your Airpush code here.
airpush.startLandingPageAd();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I hope this helps.
Consider overriding onPause method of Activity for executing a piece of code before the activity hides.
#Override
protected void onPause() {
super.onPause();
airpush.startLandingPageAd();
}

Calling onKeyDown with Viewpager and Fragments

How can i make the below code work with a viewpagers that is loading 3 fragments, all are webviews with different URL's. Im simply trying to make it so that when clicking back on whatever fragment, it calls web.goBack();
Here is the code i've used in normal activities, I guess i need recommendations on weather this code should be inside the indivisual fragments or inside the main viewpager. Any suggestions or examples, or code would be greatly appreciated.
public boolean onKeyDown(int KeyCode, KeyEvent event) {
if ((KeyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
You can override this in main Activity and then in on onKeyDown(int KeyCode, KeyEvent event) get your ViewPagers current fragment. In the end you will able to do something like this :
public boolean onKeyDown(int KeyCode, KeyEvent event) {
//.. get your fragment
WebView web = fragment.getWeb();
if ((KeyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
Best wishes.

"Back" doesn't work when start the mapview activity

why my back button doesn't function in google map activity ?
already include:
#Override
public void onBackPressed() {return;}
but still doesn't work.
You need this:
#Override
public void onBackPressed() {
super.onBackPressed();
}
Or this pre API level 5:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
// your implementation here
// finish(); - to exit the Activity
return true; // shows you consumed the event with your implementation
}
return super.onKeyDown(keyCode, event);
}
write
super.onBackPressed();
or
this.finish();
instead of return;

Categories

Resources