Skip to content

Commit

Permalink
Merge pull request #24 from livefront/make-initial-launch-clearing-safer
Browse files Browse the repository at this point in the history
Make initial launch clearing safer
  • Loading branch information
brian-livefront committed Oct 8, 2018
2 parents c46d740 + 0df6827 commit e04ddba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bridge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 12
minSdkVersion 14
targetSdkVersion 25
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Bundle;
import android.support.annotation.RequiresApi;

@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
abstract class ActivityLifecycleCallbacksAdapter implements ActivityLifecycleCallbacks {

@Override
Expand Down
29 changes: 14 additions & 15 deletions bridge/src/main/java/com/livefront/bridge/BridgeDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.support.annotation.NonNull;
Expand All @@ -27,7 +26,7 @@ class BridgeDelegate {
private static final String KEY_UUID = "uuid_%s";

private boolean mIsClearAllowed = false;
private boolean mIsFirstRestoreCall = true;
private boolean mIsFirstCreateCall = true;
private Map<String, Bundle> mUuidBundleMap = new HashMap<>();
private Map<Object, String> mObjectUuidMap = new WeakHashMap<>();
private SavedStateHandler mSavedStateHandler;
Expand Down Expand Up @@ -95,17 +94,24 @@ private Bundle readFromDisk(@NonNull String uuid) {

@SuppressLint("NewApi")
private void registerForLifecycleEvents(@NonNull Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Below this version we'll simply never allow clearing because we don't have a great
// hook for knowing when a config change is happening.
mIsClearAllowed = false;
return;
}
((Application) context.getApplicationContext()).registerActivityLifecycleCallbacks(
new ActivityLifecycleCallbacksAdapter() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
mIsClearAllowed = true;

// Make sure we clear all data after creating the first Activity if it does
// does not have a saved stated Bundle. (During state restoration, the
// first Activity will always have a non-null saved state Bundle.)
if (!mIsFirstCreateCall) {
return;
}
mIsFirstCreateCall = false;
if (savedInstanceState == null) {
mSharedPreferences.edit()
.clear()
.apply();
}
}

@Override
Expand All @@ -119,14 +125,7 @@ public void onActivityDestroyed(Activity activity) {
}

void restoreInstanceState(@NonNull Object target, @Nullable Bundle state) {
boolean isFirstRestoreCall = mIsFirstRestoreCall;
mIsFirstRestoreCall = false;
if (state == null) {
if (isFirstRestoreCall) {
mSharedPreferences.edit()
.clear()
.apply();
}
return;
}
String uuid = mObjectUuidMap.containsKey(target)
Expand Down

0 comments on commit e04ddba

Please sign in to comment.