MFlisar / GDPRDialog

GDPR fragment dialog implementation
Apache License 2.0
211 stars 53 forks source link

Sometime Dialog Activity is created 2 times #47

Closed Luklek closed 6 years ago

Luklek commented 6 years ago

Sometime the DemoGDPRActivity.startActivityForResult(this, mSetup, data.getLocation(), DemoGDPRActivity.class, DEMO_GDPR_ACTIVITY_REQUEST_CODE); creates 2 activities on top of each other. After clicking on the first, the second is displayed and need to click again on consent.

Any idea on how I could track/check or reproduce this?

MFlisar commented 6 years ago

the startActivityForResult will only create one instance. Probably the function is called twice?

Luklek commented 6 years ago

Can't reproduce anymore.

Luklek commented 6 years ago

Actually it happened again just now... I had added a log in onConsentNeedsToBeRequested and it's called twice.

MFlisar commented 6 years ago

Where do you call GDPR.getInstance().checkIfNeedsToBeShown(...) from? Make a log before this one as well and check if this is called twice... I don't see any way that this happens if it is not called twice as well

Luklek commented 6 years ago

Yes, you are right. I am calling GDPR.getInstance().checkIfNeedsToBeShown(...) onCreate and the Log onCreate was called twice too.

MFlisar commented 6 years ago

I would suggest following:

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   GDPR.getInstance().checkIfNeedsToBeShown(...)
   ...
}
@Override
public void onDestroy() {
   ...
   GDPR.getInstance().cancelRunningTasks();
   ...
}

If you use a dialog, this is not necessary, because the dialog only shows itself if it is not created already, even after activity recreation... The activity does not have this.

The best would be to cancel the task that checks the location in the onDestroy event, so you are save to just check it again whenever the activity is created again...

Luklek commented 6 years ago

You mean to add GDPR.getInstance().cancelRunningTasks(); onCreate of the "DemoGDPRActivity"?

I also added android:launchMode = "singleInstance" in Manifest of my main activity as I found some reports of onCreate called 2 times on Samsung phones (which I am using for testing too)

MFlisar commented 6 years ago

Should work as well...

I will additionally check the activity.isFinishing() in the next update before the async task will emit it's result to the activity...

Still, in an activity it makes sense to manually cancel the task (I would still suggest in onDestroy)

Luklek commented 6 years ago

OK.

Luklek commented 6 years ago

I updated to latest snapshot with the activity.isFinishing() and also added

@Override
    public void onDestroy() {
        GDPR.getInstance().cancelRunningTasks();
        Timber.d("onDestroy");
        super.onDestroy();
    }

but the DialogActivity is launched a second time anyway. (it happens in very rare case)

E/RealSplashActivity:57: GDPR Consent = UNKNOWN E/GDPRDemo [GDPR]: consent check needed: true, current consent: { Consent: UNKNOWN | Location: UNDEFINED | Date: Jan 1, 1970 07:00:00 | Version: 0} E/GDPRDemo [PreperationAsyncTask]: GDPRPreperationData: { Location: UNDEFINED | SubNetworks: 0 | Error: false } E/RealSplashActivity:126: GDPR onConsentNeedsToBeRequested D/RealSplashActivity:108: onDestroy E/RealSplashActivity:57: GDPR Consent = UNKNOWN E/GDPRDemo [GDPR]: consent check needed: true, current consent: { Consent: UNKNOWN | Location: UNDEFINED | Date: Jan 1, 1970 07:00:00 | Version: 0} E/GDPRDemo [PreperationAsyncTask]: GDPRPreperationData: { Location: UNDEFINED | SubNetworks: 0 | Error: false } E/RealSplashActivity:126: GDPR onConsentNeedsToBeRequested

MFlisar commented 6 years ago

If you have two activities, they of course each have their own AsyncTask... you must find out why the RealSplashActivity is created twice... I don't know whiy this happens, sorry

Luklek commented 6 years ago

What you mean that I have 2 activities? just the RealSplashActivity

Anyway I guess I will use the BottomSheet dialog to make sure.

MFlisar commented 6 years ago

your RealSplashActivitys onCreate is called twice, isn't it?

Luklek commented 6 years ago

Yes, that's why GDPR.getInstance().checkIfNeedsToBeShown(...) is called 2 times too, as you suggested it may be the case before (otherwise I may have not noticed that^^)

Anyway using BottomSheet the dialog is created only 1 time, so I think I will use that instead.