开发者

Widget causes app to hit onCreate

I have a home screen widget with a button, that when clicked开发者_如何学JAVA on, starts my main activity.

Every time I click on the widget, my main activity launches and runs through onCreate.

However, if I open my app from the app drawer, it doesn't run through onCreate every time. Only the first time after I install it. But the widget does it every time.

Is there a way to fix this?


Yes there is :-)

Add the attribute

android:launchMode="singleTop"

to the activity you don't want to call onCreate everytime. LaunchMode

or

add the flag to your startActivity(Intent):

Intent.FLAG_ACTIVITY_SINGLE_TOP

This will make the Activity start in the method onNewIntent() and not go through onCreate

:-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜