i have a problem with my web view in fragment that every time press back lead to home page can you help me to make back button goes to previous page .
my fragment jave file includes:-
package com.example.catalogmavbar.ui.cat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.fragment.app.Fragment;
import com.example.catalogmavbar.R;
public class CatFragment extends Fragment {
public CatViewModel catViewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cat, container, false);
WebView webView = (WebView)view.findViewById(R.id.webviewncat);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.catalogmasr.com/categories");
return view ;
}
}
You can make a function in fragment to control webview.goBack()
that called by onBackPressed()
in activity.
Example
in Fragment
CatFragment {
...
public boolean onBackPressedHandled(){
if (webview.canGoBack()) {
webview.goBack();
return true;
}
return false;
}
}
in Activity:
Activity {
...
public void onBackPressed() {
if (!catFragment.onBackpressedHandled()) {
super.onBackPressed();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.