rafi0101 / Android-Room-Database-Backup

Simple tool to backup and restore your room database in Android
MIT License
239 stars 19 forks source link

java.lang.IllegalStateException When trying to backup/restore through fragment #15

Closed raghavtilak closed 2 years ago

raghavtilak commented 2 years ago

Describe the bug I just added the java code snippet for backup, but the app crashed with the following error, on googling a bit found that this must be because of performing the registerForActivityResult on the activity. LOGS-

java.lang.IllegalStateException: LifecycleOwner com.raghav.blocker.MainActivity@f1dbe45 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED. at androidx.activity.result.ActivityResultRegistry.register(ActivityResultRegistry.java:123) at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:682) at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:691) at de.raphaelebner.roomdatabasebackup.core.RoomBackup.(RoomBackup.kt:696) at com.raghav.blocker.fragment.HomeFragment.lambda$onCreateView$6$HomeFragment(HomeFragment.java:98) at com.raghav.blocker.fragment.-$$Lambda$HomeFragment$S6AjFVYy3Pf4TKkt8gAc9w2-umY.onClick(Unknown Source:2) at android.view.View.performClick(View.java:7189) at android.view.View.performClickInternal(View.java:7166) at android.view.View.access$3500(View.java:819) at android.view.View$PerformClick.run(View.java:27682) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7592) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Expected behavior Should have shown the dialog for file location,and have backed up the db.

Screenshots Capture

Smartphone (please complete the following information):

rafi0101 commented 2 years ago

Hi @raghavtilak, you need to init the RoomBackup before calling the onClickListener. See Documentation and Sample.

raghavtilak commented 2 years ago

Hii @rafi0101 Please see my current code, I have instantiated the roombackup outside the listener, but am still getting the same error.

    RoomBackup roomBackup = new RoomBackup(requireActivity());
    roomBackup.database(AppDatabase.getInstance(requireActivity()));

    binding.backupLocally.setOnClickListener(view -> {

        roomBackup.backupLocation(RoomBackup.BACKUP_FILE_LOCATION_CUSTOM_DIALOG);
        roomBackup.maxFileCount(5);
        roomBackup.onCompleteListener((success, message, exitCode) -> {

        });
        roomBackup.backup();
    });
rafi0101 commented 2 years ago

Oh I see you are using this within a fragment. I haven't tested this yet.

Have you tried initiating it yet like in the Java example line 207 and 212? Otherwise, I would take a look at it in the next few days.

raghavtilak commented 2 years ago

Oh I see you are using this within a fragment. I haven't tested this yet.

Have you tried initiating it yet like in the Java example line 207 and 212?Otherwise, I would take a look at it in the next few days.

Yes I have tried it initialising like this, but still the same error. Ohk! Please have a look.Thanks

rafi0101 commented 2 years ago

I tested it with fragments, and found a way to fix your problem.

You need to initialize RoomBackup in the parent Activity from your Fragment. For further information see the updated doculmentation.

Please tell me if this works for you

raghavtilak commented 2 years ago

Yeah this works!! 😄 Thanks 👍🏻