On notification I start an activity which loads a webview. It works fine for the first time, but when I get a notification for the second time, it opens up a new webview, on top of the existing one.
I want to be able to just bring the activity to front and not create a new webview, since the page is already open in the background.
Function to handle notifications :
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.");
}
My web view activity :
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);
}
}
I tried using SaveInstanceState and restoreState of webview, but it didn't work.