简体   繁体   中英

Android Activity not resuming when App back from Background

I have an Activity in a FragmentActivity , which successfully loads a WebView . When the application is pushed to the background and recalled, the Activity is lost and the onResume of the FragmentActivity is called. Is there a way to keep the Activity up instead of it disappearing? Or is this because of the ProgressDialog displaying the result in the Webview ?

The call to start the Activity looks like:

                    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);

The Activity itself:

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);
        }
    }
}
}

Turns out this was a problem in the AndroidManifest.xml file. What I had:

 <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> 

Whereas it should have been:

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

The extra settings were causing the Activity to shutdown when the Application what pushed to the background.

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.

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