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