[mono-android] How to force an exit gracefully
Jonathan Pryor
jpryor at novell.com
Wed Mar 30 11:24:32 EDT 2011
On Mar 30, 2011, at 10:09 AM, Brian Long wrote:
> The flow is that after a splash screen, the main activity starts and
> when (in OnResume) it detects a lack of user login, it launches the
> login activity
> While in the login screen, the user can press the back button, which
> will go to the main activity and therefore be immediately bounced to
> the login screen again
> To avoid Back having effectively no function (in the user's eyes) I
> figured maybe Back should just exit the app
I would instead suggest that your main activity launch the login activity using Activity.StartActivityForResult()[0] with a requestCode >= 0. The login activity would then use Activity.SetResult()[1] to specify success/failure and call Activity.Finish()[2] to dismiss the login activity, and your main activity would override Activity.OnActivityResult()[3] to see if the login activity succeeded or failed; if the login activity failed, the main activity can call Activity.Finish() to dismiss itself as well.
Now, why shouldn't you call Environment.Exit()/etc.? Because processes aren't cheap -- it requires loading libmonodroid.so, the Mono JIT, loading assemblies, etc. This is why the initial launch of a Mono for Android device is ~3s on hardware, yet Back Button + restart is nigh instantaneous -- the process is being reused. You really don't want to screw with this unless you have a really good reason.
- Jon
[0] http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.StartActivityForResult(Android.Content.Intent%2cSystem.Int32)
[1] http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.SetResult(Android.App.Result)
[2] http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.Finish
[3] http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.OnActivityResult(System.Int32%2cAndroid.App.Result%2cAndroid.Content.Intent)
More information about the Monodroid
mailing list