Workmanager launching each time at launch without doing anything

Viewed 81

First time using Stackoverflow !

I have an issue with my Workmanager and I'm asking for help:

When I run my app, its executing without switching my switch to ON. It's happening every time when I install (run) my app, my notification appears without doing anything. (it still works when I'm using my switch after launching )

MyWorker.java

public class MyWorker extends Worker {

    private Workmate workmate;
    private String messageBody;


    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        retrievesWorkmateData();
        return Result.success();
    } 

SettingsActivity.java

@Override
    public int getLayout() {
        return R.layout.activity_settings;
    }

    @Override
    protected void onConfigureDesign() {

        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        configureToolbar();
        spinnerLanguage();


        final SharedPreferences.Editor editor = mSharedPreferences.edit();
        boolean notificationBoolean = mSharedPreferences.getBoolean(BOOLEAN, false);

        final OneTimeWorkRequest simpleRequest = new OneTimeWorkRequest.Builder(MyWorker.class)
                .build();
        UUID workId = simpleRequest.getId();

        if (notificationBoolean) {
            mSwitch.setChecked(true);
        }

        mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (mSwitch.isChecked()) {
                editor.putBoolean(BOOLEAN, true);
                WorkManager.getInstance().enqueue(simpleRequest);
                Toast.makeText(this, getResources().getString(R.string.Alarm_manager_start), Toast.LENGTH_SHORT).show();
            } else {
                editor.putBoolean(BOOLEAN, false);
                WorkManager.getInstance().cancelWorkById(workId);
                Toast.makeText(this, getResources().getString(R.string.Alarm_manager_cancel), Toast.LENGTH_SHORT).show();
            }
            editor.apply();
        });
    }

Have a nice day.

1 Answers

inside onConfigureDesign you are creating OneTimeWorkRequest an obtain its UUID workId = simpleRequest.getId(). this id is used inside OnCheckedChangeListener for starting or canceling work for WorkManager. consider this scenario: app doesn't have any pending work to do and user navigates to this switch and schedule work with given UUID. then user quits app, even kill it, and then got back to app and navigates again to this switch. onConfigureDesign is called again, new OneTimeWorkRequest is created and it have new UUID. this id won't cancel your previously set request, because it had/have another id... still switch is checked, as its checked/unchecked state is basing just on some boolean in SharedPreferences

solution would be to store in SharedPreferences this UUID (String in fact, use toString() and fromString(str)) when work is scheduled and remove it from there when canceling or doWork() gets called. initial state of switch should be also set basing on presence of this (any) id in shared prefs

welcome on SO :)

Related