android eclipse webview back navigation - android

hello i need to add back button to my app can anyone help me?
here is my code
public class Showing_now extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = (View) inflater.inflate(R.layout.activity_showing_now, container, false);
WebView webView = (WebView) mainView.findViewById(R.id.webViewsn);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.loadUrl("http://www.mywebsite.com");
return mainView;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://m.youtube.com")||url.startsWith("http://www.youtube.com")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}
}
}

Step 1
Add a button to your layout R.layout.activity_showing_now
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back" />
Step 2
Initialise it as normal in onCreateView
Button myButton = (Button) mainView.findViewById(R.id.myButton);
Step 3
Put an onClickListener on it like this:
myButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if (webView.canGoBack())
webView.goBack();
}
});

Related

Hyperlink click in WebView within fragment should open in new WebView in activity (tricky)

I am using fragment with WebView. Webview opens up correctly. My intention is as follows: Visitor clicks on any of the links within WebView inside the Fragment. Clicked link opens up in new WebView within new Activity. Tricky part - all links are from the same web site, there are no external links. Tricky for me, at least. Many thanks for your help in advance! :)
Here is my WebView code from Fragment:
public class HomeFragment extends Fragment {
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_naslovna, container, false);
WebView webView = view.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://mywebpage.com/");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
return view;
}
}
Please try this:
WebViewClient webClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if( url.equals("http://test.com") ){
// do whatever you want e.g. open up activity with web view and pass the url
}
return true;
}
}
It took 6 more hours u puzzle work, but this is how it got fixed at the end.
public class HomeFragment extends Fragment {
WebView webView;
SwipeRefreshLayout swipeToRefreshLayout;
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_naslovna, container, false);
webView = view.findViewById(R.id.webView1);
final String pageUrl = "https://mywebpage.com/";
swipeToRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swiperefresh);
swipeToRefreshLayout.setOnRefreshListener(new RtvTkSwipeToRefreshListener(webView, swipeToRefreshLayout));
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != pageUrl) {
startNewActivity(url);
}
return true;
}
});
webView.loadUrl(pageUrl);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
return view;
}
private void startNewActivity(String url) {
Intent myIntent = new Intent(getActivity(), NewActivity.class);
myIntent.putExtra("url", url);
getActivity().startActivity(myIntent);
}
}

Override back button while viewing webview (fragment)

I created a WebView using Fragment; here is my code:
public class MyWebView extends Fragment {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = getArguments();
String url = args.getString("url");
webView = (WebView) getView().findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setSaveFormData(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.web_view, container, false);
view.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
return view;
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
I tried to override onBackPressed() but it seems not to work with Fragment. So I used setOnKeyListener() but it doesn’t work.
Can you tell me why the back button closes the app instead of going back to the previous web page?
The Fragment doesn't receive a callback of the OnBackPressed event. You'll have to make your Activity state aware when your fragment is displayed and communicate back and forth between the activity and the fragment.
You could make your code you're calling in on key a public boolean function for instance and if the fragment is present you interact with it, you continue to return true or false in a similar way which allows you to know on the activity if you should continue with it naturally if there are no more pages to go back.

Button back in my Webview cause output

Very good at all.
I have a fragment is called a drawerlist. Within this fragment I have a webView showing a web page.
I can surf the web, but the problem comes when I want to return to the previous page, and pulse on the back button.
The result I get is that pressing the back button, the application closes. And what I want is to return to the previous page.
Code fragment is as follows:
public class BClasesWebViewFragment extends Fragment {
private WebView webView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
WebSettings webSettings = this.webView.getSettings ();
webSettings . setJavaScriptEnabled ( true );
webSettings . setDomStorageEnabled ( true );
this . webView . setWebViewClient ( new WebViewClient ());
this . webView . setWebChromeClient ( new WebChromeClient ());
webSettings . setLoadWithOverviewMode ( true );
webSettings . setUseWideViewPort ( true );
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://google.com/");
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bclases_webview, container, false);
webView = (WebView)view.findViewById(R.id.webViewBClases);
return view;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
Thank you very much everyone.
I learned a lot on this site
Try this
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;
}
});
this will work perfectly.

Save state of webpage in webview inside fragment

Using Navigation Drawer calling webpage inside webview. But when moving through the menu its reloading whole website and again and again. I just want to save the state of webpage from where user left not from start of the webpage.
Thanks for any Help.
code:-
public class Facebook extends Fragment {
// this Fragment will be called from MainActivity
public Facebook(){}
private WebView webView ;
private Bundle webViewBundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.facebook, container, false);
webView = (WebView) rootView.findViewById(R.id.facebook);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
if (webViewBundle == null) {
try{
webView.loadUrl("http://www.facebook.com");
}catch (Exception e){
e.printStackTrace();}
} else {
webView.restoreState(webViewBundle);
}
return rootView;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
Have you tried setting the Cache Mode to be LOAD_CACHE_ONLY after you loaded the website for the first time?
WebSettings webSettings = webView.getSettings();
webSettings.setCacheMode(LOAD_CACHE_ONLY);
Before getting frustrated or pulling your hair out add this next to viewpager.
ViewPager.setOffscreenPageLimit(3);// three is my number of fragments.

WebView inside Fragment - Back button?

I created a small Android 4 application with fragments.
In all these fragments I have some webviews and if I click a different fragment I want a different website to load.
I have right now TabOne.java ; TabTwo.java ; TabThree.java and so on.
I was also wondering : Can I make this application without needing a TabOne.java, TabTwo.java and so on; only from MainActivity.java? I mean, to have a single WebView, and when clicking different Tabs, this WebView to load different Url (according to the selected Tab).
If this is not possible, can you tell me what is wrong here (TabOne.java) :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tabone, container, false);
myWebView = (WebView) view.findViewById(R.id.webview);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.loadUrl("http://m.stirileprotv.ro");
myWebView = new WebView(getActivity());
myWebView.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
return false;
}
});
myWebView.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("m.stirileprotv.ro")) {
myWebView.loadUrl(url);
return true;
}
else
{
return false;
}
}
});
return view;
}
This is what i did.
In the fragment, declare the webview reference as static-
public static WebView mWebView;
public WebFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_web, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
public boolean canGoBack() {
return mWebView.canGoBack();
}
public void goBack() {
mWebView.goBack();
}
}
Now, In the parent activity -
#Override
public void onBackPressed() {
WebFragment fragment = new WebFragment();
if (fragment.mWebView.canGoBack()) {
fragment.mWebView.goBack();
} else {
super.onBackPressed();
}
}
What I did was implement it within the activity and then have a public static so:
In the main activity:
public class MainActivity extends FragmentActivity{
public static WebView myWebView;
...
#Override
public void onBackPressed() {
if (getSupportFragmentManager().findFragmentByTag("yourtag") != null) {
if(myWebView.canGoBack())
myWebView.goBack();
else {
super.onBackPressed();
}
}
else
super.onBackPressed();
}
...
}
and to reference it within the fragment:
MainActivity.myWebView = (WebView) getView().findViewById(R.id.webview);
and be sure when you create the fragment you add a tag
transaction.replace(R.id.yourfragid, newfragment, "yourtag");

Categories

Resources