[英]Android Activity not resuming when App back from Background
我在FragmentActivity
有一個Activity
,可以成功加載WebView
。 將應用程序推送到后台並重新調用時,Activity丟失,並且FragmentActivity
的onResume
被調用。 有沒有辦法使活動保持活躍而不消失呢? 還是這是因為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.