GCX-HCI / tray

a SharedPreferences replacement for Android with multiprocess support
Apache License 2.0
2.29k stars 273 forks source link

Preferences class's onCreate is called more than once? #118

Open zhutq opened 6 years ago

zhutq commented 6 years ago

tray Version (E.g. 0.12.0)

0.12.0

How have you setup tray (E.g. Initialized in Application.onCreate, in an Activity, BroadcastReceiver, IntentService, MainThread)

Create new instance in Activity/Fragment/Service.

Device(s) (E.g. Samsung Galaxy S8)

Firefly-rk3288

Android Version (E.g. Marshmallow or better API 23)

Android 5.1.1, API 22

Description (Just a place with additional information, more == better)

I create a module and migrate preferences from SharedPreferences:

public class Preferences extends TrayPreferences {
    public Preferences(final Context context) {
        super(context, "MODULE_NAME", 1);
    }

    @Override
    protected void onCreate(int initialVersion) {
        super.onCreate(initialVersion);

        String[] keys = {
            // keys ..
        };
        SharedPreferencesImport[] imports = new SharedPreferencesImport[keys.length];
        for (int i = 0; i < keys.length; i++) {
            imports[i] = new SharedPreferencesImport(getContext(), "Settings", keys[i], keys[i]);
        }
        this.migrate(imports);
        Log.d("TAG", "Migrated from SharedPreference");
    }
}

Preferences is used in two Services (each has its own process) and several Activies. These Services and Activities each creates an instance of Preference in its onCreate method and use the instance.

I notice that "Migrated from SharedPreference" is logged more than once. Am I all good? Thank you very much!