Your First Kotlin Android App · Your First Debug | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4936497-your-first-kotlin-android-app/lessons/31

While going through the tutorial, I rotated the simulator to follow the instructor, but my app did not rotate to match. Turns out I had to turn on app rotation in the settings of the OS in the simulator to make it work.

Just posting in case this helps anyone else in the future.

@zorn Thank you for the heads up - much appreciated!

I found that when I rotated, the game worked properly and didn’t reset.

I think the onSaveInstanceState(outState: Bundle) isn’t being called for some reason as I don’t get a log notification.

To test, you can set a breakpoint in the gutter, and then run the app in debug mode. Rotate the emulator and see if the method is being tripped. A shorter method is to just print a message to the console (although I find console debugging to be painful - especially using logcat)

Thanks for the reply, and agreed with the console debugging.

I find that my override fun onCreate(savedInstanceState: Bundle?) { method is being called fine, but rotating the screen is still not calling the override fun onSaveInstanceState(outState: Bundle) { method. I think this is why my app works fine when being rotated, without setting up saveInstanceState variables.

OnCreate is not being called when I rotate the screen either, just initially when the app launches.

I tried adding android:configChanges=“orientation” to the android manifest (wasn’t there) but nothing changed.

Edit: I downloaded the course materials and tested the code for myself. Same deal. Must be my android studio or module configuration?

If you’d like, feel free to send the project over to brian@razeware.com. I’ll take a look at it and see what’s happening. Sometimes, it’s easier just looking at the code. :slight_smile:

@alexhladun Do you still have issues with this?

@rbondoc Can you please help with this when you get a chance? Thank you - much appreciated! :]

TL;DR - Make sure rotation on your device/emulator is enabled.

Still seeing this. onCreate is only called on startup both on the emulator and my actual phone (verified by log statement / breakpoint).

To troubleshoot, I added android:configChanges=“orientation|screenSize|screenLayout” to allow me to manually handle as a configuration change. Activity should then call onConfigurationChanged on a rotation.

Making that change to the manifest, however, I now get neither onCreate nor onConfigurationChanged callbacks.

The activity is clearly being recreated because state is reset - but no there’s obvious way to handle it. I’ve done some Android dev in the past and don’t recall running into this before.

API change? I’m using minSDK of 19 and targetSDK of 29. Android Studio 4.0.1.

Update: I went back and tried to debug on device again and it does seem to be working there. So, I am getting onCreate on a test device but not on the emulator. So weird.

Final Update: I finally noticed that the view on the emulator was not actually rotating and that was the clue to pull down the navbar on the emulator and enable rotation - which was off. I’m an iOS dev and don’t carry an Android so did not catch this. Leaving my stream-of-consciousness comments here in case anyone else encounters this issue.

@bladebunny91 Thank you for sharing your solution - much appreciated!