繁体   English   中英

当应用从后台返回时,Android Activity无法恢复

[英]Android Activity not resuming when App back from Background

我在FragmentActivity有一个Activity ,可以成功加载WebView 将应用程序推送到后台并重新调用时,Activity丢失,并且FragmentActivityonResume被调用。 有没有办法使活动保持活跃而不消失呢? 还是这是因为ProgressDialog在Webview显示结果?

启动活动的调用如下所示:

                    Bundle bundle = new Bundle();
                    bundle.putString("fieldKey", "");
                    bundle.putString("url", menu.getUrl());
                    Intent captureIntent = new Intent(LibraryListActivity.this, WebLinkActivity.class);
                    captureIntent.putExtras(bundle);
                    LibraryListActivity.this.startActivity(captureIntent);

活动本身:

public class WebLinkActivity extends Activity {
    WebView mWebView;
    private Button mCancel;
    private String mUrl;
    ProgressDialog progressDialog;

public void clearCookies() {
    try {
        android.webkit.CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    } catch (Exception ex) {
    }
}

public void closeWebLinkClick(View view) {
    Utilities.logInfo("closeWebLinkClick", "Close WebLink Full Screen");
    setResult(Activity.RESULT_CANCELED, null);
    finish();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setResult(Activity.RESULT_OK, null);
    Bundle extras = getIntent().getExtras();
    mUrl = extras.getString("url");

    // Setup the web view. It will redirect to SSO site for login
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_weblink);

    mCancel = (Button) findViewById(R.id.closeWebLink);
    mCancel.setTextColor(Color.WHITE);

    mWebView = (WebView) findViewById(R.id.weblinkViewer);

    progressDialog = new ProgressDialog(WebLinkActivity.this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();


    // Link not provided
    if (Utilities.stringIsBlank(mUrl)) {
        String dataString = "<head><style type='text/css'>"
                + "body{margin:auto auto;text-align:center;vertical-align: middle;} </style></head>"
                + "<body><img src=\"invalid_link.png\"/></body>";
        mWebView.loadDataWithBaseURL("file:///android_res/drawable/", dataString, "text/html", "utf-8", null);
        if (progressDialog != null)
            progressDialog.dismiss();
    }
    // Network is not available
    else if (!Utilities.isNetworkAvailable()) {
        String dataString = "<head><style type='text/css'>"
                + "body{margin:auto auto;text-align:center;vertical-align: middle;} </style></head>"
                + "<body><img src=\"not_connected.png\"/></body>";
        mWebView.loadDataWithBaseURL("file:///android_res/drawable/", dataString, "text/html", "utf-8", null);
        if (progressDialog != null)
            progressDialog.dismiss();
    }
    // Normal processing
    else {
        // See if url is missing http. If so add it in
        if (!mUrl.toLowerCase().contains("http")) {
            mUrl = "http://" + mUrl;
        }

        // Most likely an image is ends with image attribute
        if (Utilities.isImage(mUrl)) {
            String html = "<html><body><img src=\"" + mUrl + "\" width=\"100%\"/></body></html>";
            mWebView.loadData(html, "text/html", null);
            if (progressDialog != null)
                progressDialog.dismiss();
        }
        // Normal web view
        else {
            mWebView.setWebChromeClient(new WebChromeClient() {
                public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                    // callback.invoke(String origin, boolean allow, boolean remember);
                    callback.invoke(origin, true, false);
                }
            });

            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            });
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setAppCacheEnabled(true);
            // HTML5 API flags
            mWebView.getSettings().setAppCacheEnabled(true);
            mWebView.getSettings().setDatabaseEnabled(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadUrl(mUrl);
        }
    }
}
}

原来这是AndroidManifest.xml文件中的问题。 我所拥有的:

 <activity
        android:name="com.mycompany.myapp.WebLinkActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|keyboard|navigation"
        android:excludeFromRecents="true"
        android:finishOnTaskLaunch="true"
        android:label="Web Link"
        android:noHistory="true">
    </activity> 

鉴于它应该是:

     <activity
        android:name="com.mycompany.myapp.WebLinkActivity"
        android:configChanges="orientation|keyboardHidden|screenSize|keyboard|navigation"            android:label="Web Link"
        android:label="Web Link">
    </activity> 

当应用程序推送到后台时,额外的设置导致Activity关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM