在Restart() 上启动动画的正确方法是什么?
目前,我在所有活动中都使用它:
@Override
public void onRestart(){
onStart();
onResume();
runFadeInAnimation();
}
它“有效”,但我不知道这是否会在将来把我搞砸。我不确定它是否是侥幸起作用,或者是否应该这样做。我尝试通过以下方式运行它:
@Override
public void onRestart(){
super.onRestart();
runFadeInAnimation();
}
但我不能——因为我的所有活动都扩展了我的主要活动,如果我通过 super 运行,它会尝试调用我的主要活动 onRestart() ,这将崩溃。 (我也希望我的主画面淡入,所以我也在那里运行动画)
我怎样才能优雅地处理这个问题并且不会在不知不觉中导致错误?
Currently, I am using this in all of my activities:
@Override
public void onRestart(){
onStart();
onResume();
runFadeInAnimation();
}
It "works" but I am clueless if this is going to screw me over in the future. I am unsure if it works by fluke or if this is how it should be done. I tried running it via:
@Override
public void onRestart(){
super.onRestart();
runFadeInAnimation();
}
But I can't-- because all of my activities extend my main activity and if I run by super it tries to call my main activities onRestart() which will crash. (I also want my main to fade in, so I am running an animation there too)
How can I handle this gracefully and not unknowingly cause a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里查看活动生命周期: http://developer.android.com/ Reference/android/app/Activity.html
您正在做的事情实际上与活动重新启动时应该自动发生的事情是多余的。
听起来您在如何定义主要活动以及如何扩展它方面可能存在一些设计问题。如果我可以提出建议,我会说,如果您的活动中的某些内容在您的所有活动中都是通用的,请将这些内容放入您可以扩展的“父”活动中。现在定义“主要”活动的方式似乎包含一些不适合其他活动的细节。因此,一旦设置了“父”活动类,就将其扩展以创建“主”活动并将“淡入”逻辑放在那里。
最终,您应该能够调用 super.onRestart 而不会产生任何有害结果。
Take a look at the Activity Lifecyle here: http://developer.android.com/reference/android/app/Activity.html
What you're doing is actually redundant with what is supposed to automatically happens on an activity restart.
It sounds like you have some possible design issues with how you've defined your main activity and how you're extending it. If I could make a suggestions, I would say that if you have things in an activity that will be common among ALL your activities, put those things in a "parent" activity that you can extend. The way you have your "main" activity defined now seems to have specifics in it that don't lend itself to other activities. So, once you set up your "parent" activity class, extend it to create the "main" activity and put the "fade-in" logic there.
Ultimately, you should be able to call super.onRestart without any detrimental results.