Android-Animated-Theme-Manager
Android-Animated-Theme-Manager copied to clipboard
Cannot start animation on a detached view for a resumed Activity.
Although I easily fixed this issue, the solution should be included in the next update.
I'd give a bit of context on why this happened and how to recreate the problem.
I have a base class BaseActivity
which extends ThemeActivity
. All my Activity
classes extend BaseActivity
and so can override syncTheme()
and getStartTheme()
.
Let's say I start from MainActivity
which extends BaseActivity
and in it's onCreate()
it calls ThemeManager.init(activity, getStartTheme())
. This happens from the ThemeActivity
super class.
However when I navigate to another class say SecondActivity
, this is what happens.
-
onStop()
ofMainActivity
is called. -
onCreate()
ofSecondActivity
is called and this is where theThemeManager
instance is initialized again (from the super classThemeActivity
) and changes privateactivity
variable fromMainActivity
toSecondActivity
. This is fine as the currentContext
would beSecondActivity
.
But when I navigate back to MainActivity
, this is what happens.
-
onStop()
forSecondActivity
is called. -
onRestart()
forMainActivity
is called. Since theMainActivity
is not destroyed and remained on the heap, it just stopped and restarted. It'sonCreate()
was not called and that means theactivity
variable in theThemeManager
instance that was reinstantiated whenSecondActivity
was navigated to still has the privateactivity
variable as theSecondActivity
instance (which should have been destroyed when navigated back up). This is a resource leak.
And so whenever I try to change theme in MainActivity
, I get a "Cannot run animation on a detached View" error from the ViewAnimator
.
I fixed this error by reinstantiating ThemeManager
by calling ThemeManager.init(activity, getStartTheme())
in the onRestart()
of MainActivity
and that fixed my issue.
So I feel onRestart()
should be overriden in ThemeActivity
and ThemeManager
should reinstantiated on that callback.
I did a debug on my code when carrying out the above scenario and found that issue out.