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
:-)
精彩评论