如何阻止创建多个Webview活动

时间:2019-05-15 08:25:05

标签: android webview push-notification

在收到通知后,我启动了一个加载Web视图的活动。 第一次可以正常运行,但是当我第二次收到通知时,它将在现有视图的顶部打开一个新的Web视图。

由于页面已经在后台打开,我希望能够将活动置于最前面而不创建新的Webview。

处理通知的功能:

    private void handleNotification() {
    final Intent notificationIntent = new Intent(this, WebViewActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(notificationIntent);
    Log.d(TAG, "Short lived task is done.");

    }

我的网络视图活动:

public class WebViewActivity extends AppCompatActivity {

private static final String TAG = "WebViewActivity";
static boolean active = false;

private Bundle savedInstanceStatee;

private void loadWebView(){
    WebView myWebView = (WebView) findViewById(R.id.webview);
    WebSettings settings = myWebView.getSettings();
    // Enable Javascript
    settings.setJavaScriptEnabled(true);
    // Allow use of Local Storage
    settings.setDomStorageEnabled(true);
    // AppRTC requires third party cookies to work
    settings.setMediaPlaybackRequiresUserGesture(false);

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptThirdPartyCookies(myWebView, true);

    if (savedInstanceStatee != null){
        Log.d(TAG,"restoring instance");
        myWebView.restoreState(savedInstanceStatee);
    }else {
        Log.d(TAG,"no saved instance");

        //Change to your URL
        myWebView.loadUrl("https://example.com");
        myWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                Log.d(TAG, "onPermissionRequest");
                Log.d(TAG,"permissions granted");
                request.grant(request.getResources());
            }
        });
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    super.onCreate(savedInstanceState);
    this.savedInstanceStatee = savedInstanceState;
    setContentView(R.layout.activity_web_view);

    //check for permissions and if granted load the webview
        loadWebView();
}

@Override
protected void onResume() {
    Log.i(TAG, "Resuming web view activity");
    super.onResume();

}

@Override
public void onStart() {
    Log.i(TAG, "Starting web view activity");

    super.onStart();
    active = true;
}

@Override
public void onStop() {
    Log.i(TAG, "Stopped web view activity");

    super.onStop();
    active = false;
}

@Override
protected void onSaveInstanceState(Bundle bundle) {
    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.saveState(bundle);
    super.onSaveInstanceState(bundle);
}
}

我尝试使用webview的SaveInstanceState和restoreState,但是没有用。

1 个答案:

答案 0 :(得分:0)

在清单中声明活动时将其启动模式更改为SingleInstance。应该可以。