我有一个片段,里面有WebView,可以打开url并登录到某些服务,然后接收一些cookie。问题是,当我打开应用程序并在活动内部进行分段时,URL正常加载到WebView中,但是当我关闭应用程序(通过activity.finishAndRemoveTask()
编程关闭)并再次(手动)打开应用程序时-URL无法加载。在下一次重新启动应用程序后(手动),一切恢复正常。我要打开的网址有两个重定向。
这是我的片段类:
public class MyFragment extends Fragment implements WebResourceClient.CookieListener {
private String URL = "https://someurl.com";
@OnClick(R.id.close)
public void detachFragment() {
FragmentManager manager = getFragmentManager();
if (manager != null) {
manager.popBackStackImmediate();
}
}
@BindView(R.id.web_container)
WebView webContainer;
@BindView(R.id.progress_bar)
ProgressBar progressBar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ny_fragment, container, false);
ButterKnife.bind(this, view);
startWebView();
return view;
}
@SuppressLint("SetJavaScriptEnabled")
private void startWebView() {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
WebSettings settings = webContainer.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
settings.setMediaPlaybackRequiresUserGesture(false);
settings.setUserAgentString(Core.USER_AGENT_FOR_GOOGLE);
settings.setAppCacheEnabled(false);
webContainer.clearCache(true);
WebView.setWebContentsDebuggingEnabled(false);
webContainer.setWebViewClient(new WebResourceClient(this));
webContainer.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress < 100) {
webContainer.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
} else {
webContainer.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
}
});
webContainer.loadUrl(URL);
}
@Override
public void onDestroyView() {
webContainer.stopLoading();
super.onDestroyView();
}
@Override
public void onReceiveCookie(String cookie) {
detachFragment();
...
}
@Override
public void showProgress() {
webContainer.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
}
}
这是我的WebViewClient:
public class WebResourceClient extends WebViewClient {
private final static String someUrl = "https://someOtherUrl";
private CookieListener listener;
public WebResourceClient(CookieListener listener) {
this.listener = listener;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains(someUrl)) {
listener.showProgress();
}
view.loadUrl(url);
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(someUrl)) {
listener.lockWebView();
}
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// if (url.contains(someUrl)) {
String cookies = CookieManager.getInstance().getCookie(url);
String cookie = getCookie(cookies);
if (!TextUtils.isEmpty(cookie)) {
view.stopLoading();
listener.onReceiveCookie(cookie);
}
super.onPageFinished(view, url);
}
private String getCookie(String cookies) {
...
}
public interface GoogleLoginListener {
void onReceiveCookie(String cookie);
void showProgress();
}
}
我在那里打开片段:
MyFragment fragment = new MyFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.addToBackStack(fragment.getClass().getSimpleName());
tr.replace(R.id.fragment_root, fragment);
tr.commit();