I am trying to implement webview back button into my app, so when the user click back button the webview return it to previos page. But when i press back button it exit the app? This is the code for webview:
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.emirnp.next.press.R;
@SuppressLint("SetJavaScriptEnabled")
public class VestiActivity extends SherlockFragmentActivity {
private static WebView webview;
private static ProgressBar bar;
private static String url;
public static class WebviewFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View v = inflater.inflate(R.layout.activity_webview, container,
false);
webview = (WebView) v.findViewById(R.id.webView1);
webview.setWebChromeClient(new MyWebChromeClient());
bar = (ProgressBar) v.findViewById(R.id.load);
url = "http://www.server.se/test/"
+ getResources().getString(R.string.vesti_id);
loadUrl();
return v;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void loadUrl() {
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
final SherlockFragmentActivity activity = getSherlockActivity();
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
bar.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT)
.show();
}
});
webview.loadUrl(VestiActivity.url);
}
}
@Override
public void onBackPressed() {
if (webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
}
WebviewActivity:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.emirnp.next.press.R;
public class WebviewActivity extends SherlockFragmentActivity {
private static WebView webview;
private static ProgressBar bar;
private static String url = null;
protected static void setUrl(String url) {
WebviewActivity.url = url;
}
public static class WebviewFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// getSherlockActivity().requestWindowFeature(Window.FEATURE_PROGRESS);
// getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
View v = inflater.inflate(R.layout.activity_webview, container,
false);
webview = (WebView) v.findViewById(R.id.webView1);
bar = (ProgressBar) v.findViewById(R.id.load);
loadUrl();
return v;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void loadUrl() {
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
final SherlockFragmentActivity activity = getSherlockActivity();
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
/*
* activity.setSupportProgress((Window.PROGRESS_END -
* Window.PROGRESS_START) / 100 * progress);
*/
if (progress == 100) {
bar.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT)
.show();
}
});
webview.loadUrl(url);
}
}
@Override
public void onBackPressed() {
if (webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
}
activity_webview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<ProgressBar
android:id="@+id/load"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>