How to add "Go Back" function in WebView inside Fragment? - android

UPDATE: Solved! Problem was related to my Viewpager not WebView.
I'm trying to add a "Go Back" function to my WebView which is inside a Fragment. But I can't figure out how to:
public final class TestFragment extends Fragment {
static WebView mWeb;
private View mContentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
mContentView = inflater.inflate(R.layout.webview, null);
mWeb = (WebView)mContentView.findViewById(R.id.webview);
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(false);
mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWeb.getSettings().setBuiltInZoomControls(false);
mWeb.loadUrl("myurl...");
mWeb.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) {
mWeb.goBack();
return true;
}
return false;
}
});
}
}
I also tried something like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) {
mWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Another solution but same problem:
#Override
public void onBackPressed()
{
if(webView.canGoBack())
webView.goBack();
else
super.onBackPressed();
}
Any ideas how to get this working?

Perhaps its android restriction. Try to do this using handler.
public final class TestFragment extends Fragment {
static WebView mWeb;
private View mContentView;
private Handler handler = new Handler(){
#Override
public void handleMessage(Message message) {
switch (message.what) {
case 1:{
webViewGoBack();
}break;
}
}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
mContentView = inflater.inflate(R.layout.webview, null);
mWeb = (WebView)mContentView.findViewById(R.id.webview);
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(false);
mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWeb.getSettings().setBuiltInZoomControls(false);
mWeb.loadUrl("myurl...");
mWeb.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWeb.canGoBack()) {
handler.sendEmptyMessage(1);
return true;
}
return false;
}
});
}
private void webViewGoBack(){
mWeb.goBack();
}
}

You can check this code :
webView.canGoBack();
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});

Actually you can not do directly inside the fragment. The onBackPressed can be overridden in the FragmentActivity. What you can do is:
Override the onBackPressed inside the activity.
When the onBackPressed is called, check if the instance of the current fragment is the instance showing the webview.
If it is, ask the fragment if the webview can go back.
if it is not, call the super or whatever you need
Edit:
#Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}

I've created a simple interface:
public interface IOnBackPressed {
boolean onBackPressed();
}
in the Activity:
public class MyActivity extends Activity {
#Override public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
if (!(fragment instanceof IOnBackPressed) || !((IOnBackPressed) fragment).onBackPressed()) {
super.onBackPressed();
}
}
}
in the Fragment:
public class MyFragment extends Fragment implements IOnBackPressed {
#Override
public boolean onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
// backpress is not considered in the Activity
return true;
} else {
// activity will act normal
return false;
}
}
}

In WebViewActivity.java, I added 1 method:
#Override
public void onBackPressed() {
WebViewFragment fragment = (WebViewFragment)
getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
if (fragment.canGoBack()) {
fragment.goBack();
} else {
super.onBackPressed();
}
}
In WebViewFragment.java, I added 2 methods:
public boolean canGoBack() {
return mWebView.canGoBack();
}
public void goBack() {
mWebView.goBack();
}

#RomanBlack's answer gave me the right idea, but since we use kotlin I had to adapt the answer a little bit.
webView.setOnKeyListener { _, _, keyEvent ->
if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK && !webView.canGoBack()) {
false
} else if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK && keyEvent.action == MotionEvent.ACTION_UP) {
webView.goBack()
true
} else true
}
if you want to do it with returns you have to add something like:
return#setOnKeyListener true

There is a simple way with BackPressedDispatcher
Fragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(webView.canGoBack()){
webView.goBack()
} else {
isEnabled = false
requireActivity().onBackPressed()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(this,callback)
}
Activity :
override fun onBackPressed() {
val fragment = supportFragmentManager.findFragmentByTag("WebViewFragment")
if (WebViewFragment::class.java.isInstance(fragment)) {
if (onBackPressedDispatcher.hasEnabledCallbacks()) {
onBackPressedDispatcher.onBackPressed()
return
}
super.onBackPressed()
}
}
Maybe these codes can be improved, but it works very well for me. For more information visit here.

my solution was in fragment I added to public methods
public static boolean canGoBack(){
return mWebView.canGoBack();
}
public static void goBack(){
mWebView.goBack();
}
then from activity I call
#Override
public void onBackPressed() {
if(webFragment.canGoBack()){
webFragment.goBack();
}else{
super.onBackPressed();
}
}
note The mwebview is static

you can do this by :
in the Activity put :
// Set WebView
public void setWebView(WebView web) {
this.web = web;
}
in the web fragment after ActivityCreated() put:
((Your_Activity) getActivity()).setWebView(webView);
Don't forget to set webView from the onCreateView() like these:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
webView = (WebView) inflater.inflate(R.layout.your_web_fragment, container,
false);
return web;
}

#OmidAmnivia answer is correct your app the solution to the crash is
#Override
public void onBackPressed() {
if(webFragment.isInitialized && webFragment.canGoBack()){
webFragment.goBack();
}else{
super.onBackPressed();
}
}
You have to check whether your class has been initialised or not.

This is how I did in my app. I consume back press event till web view can go back. Once web view cant go back I show hint to user that if he keep on pressing back then app will exit.
It will give user a chance to stay in your app when webview cant go back. I felt it more user friendly:
//time passed between two back presses.
private val TIME_INTERVAL = 200
// variable to keep track of last back press
private var mBackPressed: Long = 0
webView!!.requestFocus()
webView.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.action == MotionEvent.ACTION_UP
) {
if(webView.canGoBack()) {
//go back in previous page
webView.goBack()
return#OnKeyListener true
}
else
{
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
{ // dont consume back press and pass to super
return#OnKeyListener false
}
else {
// show hint for double back press
Toast.makeText(context, " Double Tap back button to exit the demo", Toast.LENGTH_SHORT).show();
mBackPressed = System.currentTimeMillis();
return#OnKeyListener true
}
}
}
return#OnKeyListener false
})

First of all get back pressed in fragment
mContentView.setFocusableInTouchMode(true);
mContentView.requestFocus();
mContentView.setOnKeyListener( new OnKeyListener()
{
#Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return false;
}
} );
hope it will works.

This worked in my case
public class FantasyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
WebView webview;
SwipeRefreshLayout swipeLayout;
String currentUrl="https://www.stackoverflow.com/";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_stadium, container, false);
swipeLayout = (SwipeRefreshLayout) root.findViewById(R.id.swipescreen);
swipeLayout.setOnRefreshListener(this);
return root;
}
#Override
public void onStart() {
super.onStart();
LoadWeb();
}
public void LoadWeb() {
webview = (WebView) getActivity().findViewById(R.id.webview786);
swipeLayout.setRefreshing(true);
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.getSettings().setAppCacheEnabled(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
webview.loadUrl(currentUrl);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Snackbar.make(view, "Connection Error", Snackbar.LENGTH_LONG)
.setAction("Retry", new View.OnClickListener() {
#Override
public void onClick(View v) {
LoadWeb();
}
}).show();
}
#Override
public void onPageFinished(WebView view, String url) {
swipeLayout.setRefreshing(false);
currentUrl = url;
super.onPageFinished(view, url);
}
});
webview.canGoBack();
webview.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webview.canGoBack()) {
webview.goBack();
return true;
}
return false;
}
});
}
#Override
public void onRefresh() {
LoadWeb();
}
}

The simplest answer is jut to setOnKeyListeneron into webView itself:
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if( keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
webView.goBack();
return true;
}
}
return false;
}
});

Related

Android: Back button OnKeyListener inside Fragment is not working

The onKeyListener() in a Fragment is not working now. Here's the part of the code.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);
...
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
// true if the listener has consumed the event, false otherwise.
// the key event happens twice, when pressing and taking off.
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && !touched) {
Log.i(TAG, "onKey() method");
touched = true;
return true;
} else {
return false;
}
}
});
return view;
}
Is there anything I am missing?
try this:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//go to previous fragemnt
//perform your fragment transaction here
//pass data as arguments
return true;
}
}
return false;
}
});
}
This worked for me, for both Fragment & DialogFragment.
For Fragment:
#Override
public void onResume() {
super.onResume();
// Disable back press
requireView().setFocusableInTouchMode(true);
requireView().requestFocus();
requireView().setOnKeyListener((v, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);
}
For DialogFragment:
#Override
public void onResume() {
super.onResume();
requireDialog().setOnKeyListener((dialog, keyCode, event) ->
// Disable back pressed
keyCode == android.view.KeyEvent.KEYCODE_BACK
);
}

how to disable the back button in device by implement 1 function

i am trying to disable the back button in my device with the following code.
the code is working but i would like that the function that handle all the back button request in the fragments will derived from the Main Activity
this is the back button handler:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getActivity(), "Please navigate via the menu", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
});
}
It's easier to override onBackPressed function:
private boolean disabled = true;
#Override
public void onBackPressed() {
if (!disabled) {
super.onBackPressed();
}
}
With this you can easy change disable flag and enable back button when needed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
//leave it empty
}
return super.onKeyDown(keyCode, event);
}
It's generally not the best idea to disable the back button as the user expects that button to perform some sort of back navigation. Now that being said, if you have a specific reason you can disable the back button by overriding the onBackPressed at the activity level and not calling into super.
Put this code at your MainActivity:
#Override
public void onBackPressed() {
executeAtAndroidOnBackPressed();
}
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
executeAtAndroidOnBackPressed();
}
return false;
}
protected void executeAtAndroidOnBackPressed() {
// do nothing
}

How to control navigation history in webview inside fragment

I have a webview inside fragment and I want it to be controlled by the navigation history whenever I press the back button.
In my case, when I press the back button I leave the webview, but I want that on pressing the button I will return to the state that is stored in the navigation history (from before leaving the webview).
Any solution please?
I tried with onKey but it does not resolve my problem. Here my code:
public class FragmentAll extends Fragment implements
DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
OnDownloaExpodTerminated {
public static Fragment newInstance(Context context) {
FragmentAll f = new FragmentAll();
return f;
}
private WebView myWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
(((MainActivity) getActivity()) ).setActionBarTitle("Store");
myWebView = (WebView) v.findViewById(R.id.webView1);
setWebview();
v.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if (arg1 == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return false;
}
});
return v;
}
...
}
You can try it
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
// Otherwise defer to system default behavior.
super.onBackPressed();
}
You will have to make a method inside your Fragment class:
public void goBackWebview(){
myWebview.goBack();
}
Now, go to the Activity class to which this fragment is attached and override the onBackPressed():
#Override
public void onBackPressed() {
//if webview has history
myFragment.goBackWebView();
}
you need to manage list of links is loaded in your webview.
you can get the url like this
web_des2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
HitTestResult hitTestResult = web_des2.getHitTestResult();
if (hitTestResult != null) {
String url = hitTestResult.getExtra();
if (url != null && url.startsWith("http://")) {
yourUrlList.add(url);
web_des2.loadUrl(url);
}
}
return true;
}
return false;
}
});
you can check onBackPress if your list does not empty then get url form list and load it into webview and remove from the list.
you can use Stack to store url so become easy you can use pop() and push() function directly. if like than +1

Fragment pressing back button

I am now having an activity containing fragments
[1] , [2] , [3] , [4]
If pressing buttons , [3] , it can be redirected to [4]
I would like to implement the back button as shown follow..
when pressing back at [4] , it return to [3]
when pressing back at [3] , it return to [2]
when pressing back at [1] , the activity finishes();
When it comes to the current implementation, it finish the activity instead of popping up the Fragment. Would you please tell me what I should do or keep in mind ?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return super.onKeyDown(keyCode, event);
}
This worked for me.
-Add .addToBackStack(null) when you call the new fragment from activity.
FragmentTransaction mFragmentTransaction = getFragmentManager()
.beginTransaction();
....
mFragmentTransaction.addToBackStack(null);
-Add onBackPressed() to your activity
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
} else {
getFragmentManager().popBackStack();
}
}
Easiest way ever:
onResume():
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
// handle back button's click listener
Toast.makeText(getActivity(), "Back press", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
Edit 1: If fragment having EditText.
private EditText editText;
onCreateView():
editText = (EditText) rootView.findViewById(R.id.editText);
onResume():
#Override
public void onResume() {
super.onResume();
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
editText.clearFocus();
}
return false;
}
});
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
// handle back button's click listener
Toast.makeText(getActivity(), "Back press", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
Note: It will work if you have EditText in fragment.
Done
This is a working solution for me:
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// DO WHAT YOU WANT ON BACK PRESSED
return true;
}
return false;
}
});
Edit: You can replace dialog with getView() for fragments.
Try this simple solution:
In your activity implement onBackPressed
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
finish();
}
}
This will work if you want to pop the top fragment on each back press.
Note:- While adding fragment to activity always do add the transaction to back stack for this to work
In your onCreate() in your activity housing your fragments add a backstack change listener like so:
fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
List<Fragment> f = fragmentManager.getFragments();
Fragment frag = f.get(0);
currentFragment = frag.getClass().getSimpleName();
}
});
(Nb. my fragmentManager is declared global)
Now every time you change fragment the currentFragment String will become the name of the current fragment. Then in the activities onBackPressed() you can control the actions of your back button as so:
#Override
public void onBackPressed() {
switch (currentFragment) {
case "FragmentOne":
// your code here
return;
case "FragmentTwo":
// your code here
return;
default:
fragmentManager.popBackStack();
// default action for any other fragment (return to previous)
}
}
I can confirm that this method works for me.
Update : Kotlin
override fun onBackPressed() {
when(supportFragmentManager.fragments[0].javaClass.simpleName){
"FragmentOne" -> doActionOne()
"FragmentTwo" -> doActionTwo()
else -> supportFragmentManager.popBackStack()
}
}
Solution for Pressing or handling back button in Fragment.
The way I solved my issue I am sure it will helps you too:
1.If you don't have any Edit Text-box in your fragment you can use below code
Here MainHomeFragment is main Fragment (When I press back button from second fragment it will take me too MainHomeFragment)
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
MainHomeFragment mainHomeFragment = new SupplierHomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainHomeFragment);
fragmentTransaction.commit();
return true;
}
return false;
}
}); }
2.If you have another fragment named as Somefragment and it has Edit text-box then you can do it by this way.
private EditText editText;
Then In,
onCreateView():
editText = (EditText) view.findViewById(R.id.editText);
Then Override OnResume,
#Override
public void onResume() {
super.onResume();
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
editTextOFS.clearFocus();
getView().requestFocus();
}
return false;
}
});
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
MainHomeFragment mainHomeFragment = new SupplierHomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainHomeFragment);
fragmentTransaction.commit();
return true;
}
return false;
}
});
}
That's all folks (amitamie.com) :-) ;-)
Still better solution could be to follow a design pattern such that the back-button press event gets propagated from active fragment down to host Activity. So, it's like.. if one of the active fragments consume the back-press, the Activity wouldn't get to act upon it, and vice-versa.
One way to do it is to have all your Fragments extend a base fragment that has an abstract 'boolean onBackPressed()' method.
#Override
public boolean onBackPressed() {
if(some_condition)
// Do something
return true; //Back press consumed.
} else {
// Back-press not consumed. Let Activity handle it
return false;
}
}
Keep track of active fragment inside your Activity and inside it's onBackPressed callback write something like this
#Override
public void onBackPressed() {
if(!activeFragment.onBackPressed())
super.onBackPressed();
}
}
This post has this pattern described in detail
What I do in this cases is I implement the onBackPressed() function from the Activity:
#Override
public void onBackPressed() {
super.onBackPressed();
FragmentManager fm = getSupportFragmentManager();
MyFragment myFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
if((myFragmen.isVisible()){
//Do what you want to do
}
}
How this works for you too.
if you are using webview inside a fragment than use this in your onCreateView method
webView.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if((i==KeyEvent.KEYCODE_BACK)&& webView.canGoBack()){
webView.goBack();
return true;
}
return false;
}
});
and import this class
import android.view.KeyEvent;
You can use getFragmentManager().popBackStack() in basic Fragment to go back.
You also need to check Action_Down or Action_UP event. If you will not check then onKey() Method will call 2 times.
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getActivity(), "Back Pressed", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
});
Working very well for me.
you can use this one in onCreateView, you can use transaction or replace
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
#Override
public void handleOnBackPressed() {
//what you want to do
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
Make sure to add the following:
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return true;
in the onKey block of code to avoid the event calling twice.
i use a methode to change fragments it has thw following code
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit).replace(R.id.content_frame, mContent, mContent.getClass().getSimpleName()).addToBackStack(null)
.commit();
and for the back button this .
#Override
public void onBackPressed() {
// note: you can also use 'getSupportFragmentManager()'
FragmentManager mgr = getSupportFragmentManager();
if (mgr.getBackStackEntryCount() == 1) {
// No backstack to pop, so calling super
finish();
} else {
mgr.popBackStack();
}
}
the important thing to note is i use 1 for checking getBackStackEntryCount this is because if you dont use it and use 0 user sees nothing for the last back button.
use this (in kotlin)
activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// in here you can do logic when backPress is clicked
}
})
i think this is the most elegant way to do it
Try this, its helped me :)
#Override
public void onResume() { //Pressed return button - returns to the results menu
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
/*Here your code*/
return true;
}
return false;
}
});
}
just paste it in your activity main
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
finish();
}
}

How to go back to previous page if back button is pressed in WebView?

I have an app in which I have a WebView where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my app. But when I click the phone's back button, it takes me straight into my app. I want to go back to the previous page in the website instead. How can I do this?
Here is the code sample I'm using:
public class Webdisplay extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webdisplay);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
Toast loadingmess = Toast.makeText(this,
"Cargando El Diario de Hoy", Toast.LENGTH_SHORT);
loadingmess.show();
WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.elsalvador.com");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setInitialScale(1);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity = this;
myWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
}
I use something like this in my activities with WebViews:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Edit:
For this code to work, you need to add a field to the Activity containing the WebView:
private WebView mWebView;
Initialize it in the onCreate() method and you should be good to go.
mWebView = (WebView) findViewById(R.id.webView);
If using Android 2.2 and above (which is most devices now), the following code will get you what you want.
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
This is my solution. It works also in Fragment.
webView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
Full reference for next button and progress bar : put back and next button in webview
If you want to go to back page when click on phone's back button, use this:
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
You can also create custom back button like this:
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (wv.canGoBack()) {
wv.goBack();
}
}
});
Focusing should also be checked in onBackPressed
#Override
public void onBackPressed() {
if (mWebview.isFocused() && mWebview.canGoBack()) {
mWebview.goBack();
} else {
super.onBackPressed();
finish();
}
}
Why not use onBackPressed()?
#Override
public void onBackPressed() {
// super.onBackPressed(); Do not call me!
// Go to the previous web page.
}
here is a code with confirm exit:
#Override
public void onBackPressed()
{
if(webView.canGoBack()){
webView.goBack();
}else{
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
In kotlin:
override fun onBackPressed() {
when {
webView.canGoBack() -> webView.goBack()
else -> super.onBackPressed()
}
}
webView - id of the webview component in xml, if using synthetic reference.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
mWebView = findViewById(R.id.webView);
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
The first answer by FoamyGuy is correct but I have some additions; low reputations cannot allow me to do comments. If for some reasons your page fails to load, ensure that you set a flag to take note of the failure and then check it on the onBackPressed override. Otherwise your canGoBack() will be forever executed without heading to the actual back activity if it was there:
//flag with class wide access
public boolean ploadFailFlag = false;
//your error handling override
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
ploadFailFlag = true; //note this change
.....
.....
}
//finally to the answer to this question:
#Override
public void onBackPressed() {
if(checkinWebView.canGoBack()){
//if page load fails, go back for web view will not go back - no page to go to - yet overriding the super
if(ploadFailFlag){
super.onBackPressed();
}else {
checkinWebView.goBack();
}
}else {
Toast.makeText(getBaseContext(), "super:", Toast.LENGTH_LONG).show();
super.onBackPressed();
}
}
You can try this for webview in a fragment:
private lateinit var webView: WebView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_name, container, false)
webView = root!!.findViewById(R.id.home_web_view)
var url: String = "http://yoururl.com"
webView.settings.javaScriptEnabled = true
webView.webViewClient = WebViewClient()
webView.loadUrl(url)
webView.canGoBack()
webView.setOnKeyListener{ v, keyCode, event ->
if(keyCode == KeyEvent.KEYCODE_BACK && event.action == MotionEvent.ACTION_UP
&& webView.canGoBack()){
webView.goBack()
return#setOnKeyListener true
}
false
}
return root
}
You should the following libraries on your class handle the onBackKeyPressed.
canGoBack() checks whether the webview can reference to the previous page. If it is possible then use the goBack() function to reference the previous page (go back).
#Override
public void onBackPressed() {
if( mWebview.canGoBack()){
mWebview.goBack();
}else{
//Do something else. like trigger pop up. Add rate app or see more app
}
}
Here is the Kotlin solution:
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
if (event?.action != ACTION_UP || event.keyCode != KEYCODE_BACK) {
return super.onKeyUp(keyCode, event)
}
if (mWebView.canGoBack()) {
mWebView.goBack()
} else {
finish()
}
return true
}
If someone wants to handle backPressed for a webView inside a fragment, then he can use below code.
Copy below code into your Activity class (that contains a fragment YourFragmmentName)
#Override
public void onBackPressed() {
List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
boolean handled = false;
for(Object f: fragmentList) {
if(f instanceof YourFragmentName) {
handled = ((YourFragmentName)f).onBackPressed();
if(handled) {
break;
}
}
}
if(!handled) {
super.onBackPressed();
}
}
Copy this code in the fragment YourFragmentName
public boolean onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
return true;
} else {
return false;
}
}
Notes
Activity should be replaced with the actual Acitivity class you are using.
YourFragmentName should be replaced with the name of your Fragment.
Declare webView in YourFragmentName so that it can be accessed from within the function.
I think I'm a little late but according to the android documentation, you can have custom back navigation inside fragments done by the following piece of code.
requireActivity().onBackPressedDispatcher.addCallback(this) {
// Handle the back button event
if (binding.webView.canGoBack()){
binding.webView.goBack()
} else {
findNavController().popBackStack()
}
}
you can customize what to do when you the webView cant go back as per your needs.
Official Kotlin Way:
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// Check if the key event was the Back button and if there's history
if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
myWebView.goBack()
return true
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event)
}
https://developer.android.com/guide/webapps/webview.html#NavigatingHistory
use this code to go back on page and when last page came then go out of activity
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent=new Intent(LiveImage.this,DashBoard.class);
startActivity(intent);
}

Categories

Resources