Make WebViewFragment work with predictive back
This commit is contained in:
parent
04129920eb
commit
4e1bf80e12
|
@ -1,6 +1,7 @@
|
|||
package org.joinmastodon.android.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -10,14 +11,18 @@ import android.webkit.WebResourceRequest;
|
|||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import org.joinmastodon.android.BuildConfig;
|
||||
import org.joinmastodon.android.api.MastodonErrorResponse;
|
||||
|
||||
import me.grishka.appkit.Nav;
|
||||
import me.grishka.appkit.fragments.LoaderFragment;
|
||||
import me.grishka.appkit.fragments.OnBackPressedListener;
|
||||
|
||||
public abstract class WebViewFragment extends LoaderFragment implements OnBackPressedListener{
|
||||
public abstract class WebViewFragment extends LoaderFragment{
|
||||
private static final String TAG="WebViewFragment";
|
||||
|
||||
protected WebView webView;
|
||||
private Runnable backCallback=this::onGoBack;
|
||||
private boolean backCallbackSet;
|
||||
|
||||
@Override
|
||||
public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
|
||||
|
@ -31,18 +36,28 @@ public abstract class WebViewFragment extends LoaderFragment implements OnBackPr
|
|||
webView.setWebViewClient(new WebViewClient(){
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url){
|
||||
if(BuildConfig.DEBUG){
|
||||
Log.d(TAG, "onPageFinished: "+url);
|
||||
}
|
||||
dataLoaded();
|
||||
updateBackCallback();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
|
||||
onError(new MastodonErrorResponse(error.getDescription().toString(), -1, null));
|
||||
updateBackCallback();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
|
||||
return WebViewFragment.this.shouldOverrideUrlLoading(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){
|
||||
updateBackCallback();
|
||||
}
|
||||
});
|
||||
webView.getSettings().setJavaScriptEnabled(true);
|
||||
return webView;
|
||||
|
@ -58,19 +73,28 @@ public abstract class WebViewFragment extends LoaderFragment implements OnBackPr
|
|||
webView.reload();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBackPressed(){
|
||||
if(webView.canGoBack()){
|
||||
webView.goBack();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onToolbarNavigationClick(){
|
||||
Nav.finish(this);
|
||||
}
|
||||
|
||||
private void updateBackCallback(){
|
||||
boolean canGoBack=webView.canGoBack();
|
||||
if(canGoBack!=backCallbackSet){
|
||||
if(canGoBack){
|
||||
addBackCallback(backCallback);
|
||||
backCallbackSet=true;
|
||||
}else{
|
||||
removeBackCallback(backCallback);
|
||||
backCallbackSet=false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onGoBack(){
|
||||
if(webView.canGoBack())
|
||||
webView.goBack();
|
||||
}
|
||||
|
||||
protected abstract boolean shouldOverrideUrlLoading(WebResourceRequest req);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue