This one is tricky because I totally was lost until I luckily came across a forum post that explained how Android works behind the scenes during orientation change.
So my Android widget has two layouts, one visible and one hidden. The visible layout is default while the hidden layout will only be shown if data is present.
When orientation change happens (portrait, landscape), the problem is that the hidden layout which I programmaticaly set to visible is not shown.
Use Jona from Stack Overflow mentioned this important note that solved my problem.
“When the screen is rotated and the widget needs to be restored the Android system will restore it using the data from the last RemoteViews update. I was updating parts of the widget separately so when it needed to restore only one part got updated. The fix is always update everything on your RemoteViews all at once.”
So that is the solution. Whenever you instantiate and inflate RemoteViews, be sure that you also set the View states on everything.
This took me awhile to figure it out because whenever I navigate using the buttons in one of the widget, it affects all other instances of that widget. But thankfully, some forum member in Stack Overflow named snailer provided a solution to making each Intent object unique per instance of an Android widget.
Just add the following code to your Intent object.
The variable URI_SCHEME is a String, and can be whatever you would like. ie – “ABCD” This causes each widget to have a unique PendingIntent.
There is a big chance that you did this within the onCreate() method of the Activity class and you wondered why if with the click of the button, it works.
I read a post from the Stack Overflow forum that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity because the AnimationDrawable is not yet fully attached to the window.
A good suggestion by user will is to create an inner class where you trigger the start() method of the AnimationDrawable. Try the sample inner class below in your class. It should work.