已經有各種文章教你如何在Android程式的任何一個頁面執行退出整個程式的功能,Android.os方法、restartPackage方法、system.exit方法,甚至有人為製造異常使程式退出的方法,令人害怕呀。
其實,我們在Android model的基礎上想一下,真得需要這個功能:讓使用者在任何時候,都能看到一個直接退出該應用的按鈕嗎?
我認為不需要。Google官方人員在很早就表達了這個觀點:
Question: Does the user have a choice to kill the application
unless we put a menu option in to kill it? If no such option exists,
how does the user terminate the application?
Answert (Romain Guy): The user doesn't, the system handles this
automatically. That's what the activity lifecycle (especially
onPause/onStop/onDestroy) is for. No matter what you do, do not put a
"quit" or "exit" application button. It is useless with Android's
application model. This is also contrary to how core applications work.
讓Android架構(Activity的生命週期)去自動管理應用的生死吧,目前存在的任何一個退出的方法技巧我覺得在過去或未來的某個時候都會出現錯誤和異常,而無法完成該功能。與Android架構天然相違背的東西,還是不要在menu上出現了吧。
參考:http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238