When changing device orientation, I lose text input, how can I fix this
I have a countdown timer application, and when I change the orientation from vertical to horizontal on my phone, the countdown text no 开发者_StackOverflowlonger displays, and when I switch it back it does not come back, unless, I hit start again, which shouldn't be there in the first place, because the button changes to pause when it starts, but when I flip it, the countdown indicator goes blank and the button says start, even though the countdown is running, and displays an onFinish().
Your activity gets restarted, you'll want to override onRetainNonConfigurationInstance
which is specifically called in this scenario before the activity is destroyed. Threre's an article on the android site that covers this in detail.
that's because Dalvik vm destroy the activity and creates a new one when you device is rotating, so, you have to find someway to save the state of the activity and his childs, maybe overriding one of the methods, onFinish(), onStop() or onDestroy()
Never mind I found it. I had set my orientation in my layout:
<LinearLayout
android:orientation="vertical">
all I had to do from there on is add this to my AndroidManifest in the I was working with.
<activity android:name="TimerActivity" android:configChanges="keyboardHidden|orientation"/>
And voala!
精彩评论