'onCreate launches twice without any UI changes

I am currently developing an app with network requests. However, even though I requested once, there were two results that arrived. (Checked with logcat) After that, I began to study about Android Lifecycles, and I added logs to ALL of the Lifecycle functions:

Ex) onResume()

override fun onResume() {
    Log.d("LIFECYCLE", "OnResume Started")
    super.onResume()
    Log.d("LIFECYCLE", "OnResume Ended")
}

After printing the logcat, I got a result of this:

 1. D/LIFECYCLE: OnCreate Started
 2. W/<appname>: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
 3. W/<appname>: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
 4. D/LIFECYCLE: OnCreate Ended
 5. D/THREAD: Thread Started -> The network request thread
 6. W/<appname>: Accessing hidden method Ljava/lang/invoke/MethodHandles$Lookup;-><init>(Ljava/lang/Class;I)V (unsupported, reflection, allowed)
 7. D/LIFECYCLE: OnStart Started
 8. I/ActivityThread: Schedule relaunch activity: <PACKAGE_NAME>.activities.MainActivity
 9. D/LIFECYCLE: OnStart Ended
10. D/LIFECYCLE: OnResume Started
11. D/LIFECYCLE: OnResume Ended
12. W/<appname>: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (unsupported,core-platform-api, reflection, allowed)
13. W/<appname>: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (unsupported,core-platform-api, reflection, allowed)
14. W/<appname>: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (unsupported,core-platform-api, reflection, allowed)
15. D/LIFECYCLE: OnPause Started
16. D/LIFECYCLE: OnPause Ended
17. D/LIFECYCLE: OnDestroy Started
18. D/LIFECYCLE: OnDestroy Ended
19. D/LIFECYCLE: OnCreate Started
20. D/LIFECYCLE: OnCreate Ended
21. D/THREAD: Thread Started
22. D/LIFECYCLE: OnStart Started
23. D/LIFECYCLE: OnStart Ended
24. D/LIFECYCLE: OnResume Started
25. D/LIFECYCLE: OnResume Ended
26. D/THREAD: Thread Ended
27. D/THREAD: Thread Ended
...

D/Thread in the logcat is the network request thread that I made in onCreate()

As you can see in line 8, There is a log from ActivityThread where it says "Schedule relaunch activity". However, I did not restart nor relaunched my activity. The app is running smooth without any restarting animation or any lags while the log is printing.

I tried solutions like adding null check to savedInstanceState and run the network thread when it is null, but somehow the network request results and the UI changing commands that I made inside the thread reflects in the second request, so this was no good.

THE MOST IMPORTANT PART: To find what went wrong, I opened an emulator and tested my app. Surprisingly, when I open the app with the emulator, there is no activity relaunch log and no duplicate response. It works perfectly as I programmed.

The device I struggled with is Pixel 6 Pro (Physical Device). I want to make my app work in this typical device.

How can it be different? Why is this happening? Are there any solutions to not relaunch the activity by itself?



Solution 1:[1]

You can try to wrap your network thread in savedInstanceState != null. I had the same issue on Pixel 6 Pro with a fragment call on activity launch. The Activity will be relaunched anyway but in my case the fragment will not. Maybe this works for you, too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marblefox