Farhan
Farhan

Reputation: 3226

android: how to return

this is rather a very rare question and quite an unusual one.. In my onCreate () method, i first check if sd-card is mounted or not. if it is unmounted then i simply use return; so that no further code can be executed and the user is presented with an alert diaglog where the user can select retry or exit. i used return; in onCreate because I didn't want to process the rest of the code otherwise it might generate errors. Now I want to know two things

When I use return; in the onCreate () method, where does it return to?

Upvotes: 1

Views: 4218

Answers (2)

Erich Douglass
Erich Douglass

Reputation: 52002

When I use return; in the onCreate() method, where does it return to?

onCreate() declares a void return type, so you're not returning a value. In this case, returning early simply ends execution of the method before the remainder of the code is executed. In terms of Android, you're returning control back to the system (i.e. UI thread).

Edit: Whenever Android runs an application, it creates a process and (at a minimum) one thread (the UI thread) to run your code. That thread follows the application lifecycle and calls into your code at certain points (activity callbacks). Whenever your code isn't running, the system is free to process input events that have been queued up. To sum up a very long winder answer: whenever you return from onCreate(), onResume(), etc, you have returned control back to the system.

Upvotes: 4

Elemental
Elemental

Reputation: 7466

Sounds as if you want to end your activity from inside your OnCreate, you can do this (as you can at any time) by calling finish()

Upvotes: 1

Related Questions