'What happens when we call finish() in onCreate() method?
What happens when we call the finish()
method for the activity inside onCreate()
?
Will the app get time to display the Activity
, but then rapidly close, or won't it show anything?
This is just a doubt that I had before reading about some malware services for android that allegedly use this idea... Does it effectively work?
Solution 1:[1]
I just tested and debugged it, the Activity
starts but is immediately closes once the event is generated.
Solution 2:[2]
As MrMins mention it will rapidly close. The lifecycle will skip over some important steps, therefore causing potential issues.
But to answer you question it will run only momentarily.
Here is the android documentation of the lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle.html
Solution 3:[3]
As per official documentation:
You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.
The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop().
so the "visible lifetime" is not reached for that case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Benjamin RD |
Solution 2 | Noah |
Solution 3 | Marian Pa?dzioch |