There is an application with 2 activities: Splash screen and main screen. After installing application it shows splash, then finishes Splash activity and starts main activity. Then i test 3 scenarios:
App is launched, second activity showed on screen. I kill app process by DDMS, launch app again — everything is right — i see splash again, then second activity
App is launched, second activity showed on screen. I press back key, then kill process, launch application again — everything all right too, it launch splash first
App is launched, second activity showed on scree开发者_如何学Gon. I press home key, then kill process, launch application again — and there is surprise — application starts on second activity, escaping splash.
What`s wrong?
There is some others question like this one, but i still got no answer.
Can anybody explain this behavior?
Splash screens aren't a real good idea anyway since it gives the user the feeling that the application is an add-on and breaks slightly the whole system life cycle.
But if you really want to have a splash screen on the launch Activity, there are two options. One is to have two views inside a root RelativeLayout
. One with the splash screen inside an ImageView
and another layout with the actual content of the activity. Then, hide the ImageView
with your favorite animation.
Alternatively, you can use a fragment instead of the content layout and load the Activity
instances (What used to be done with ActivityGroup
).
Update:
Ok, I forgot to mention why is that happening in your app. In any Android application we have a loosely bound set of activities and (usually) when we launch a new Activity it gets added to an Activity stack (to manage the back behavior). I say usually because you can change that behavior if required (launchMode).
When you press the back button the stack is emptied and the activity terminated. On the other hand, when pressing the home button, the stack is saved and restored when relaunching.
The Android life cycle can be a bit of a headache sometimes, but once you understand it is really well thought through.
here is some further reading: Activity and Task Design Guidelines
Application always starts from where you were before.... Lets say you have 5 seconds. You open the app for 4 seconds with the splash screen. You close the app. You open it. There is only 1 second left for the splash screen
精彩评论