2012-08-17 77 views
0

退出本地活動的正確方法是什麼?也就是說,我該如何結束我的android_main函數。如果我什麼都不做,程序就會掛起,最終設備終止它。如果我打電話給exit(0),那麼它會退出,但它會在日誌文件中打印轉儲,表明它沒有正確清理。退出android_main的正確方法

那麼我該如何正確地退出應用程序呢?

編輯:我純粹用C++編寫本地活動。我在詢問如何返回/完成我的android_main例程。我有此項目中的Java代碼。

日誌失敗的頂部片段:

V/threaded_app(5419): NativeWindowDestroyed: 0x29c490 -- 0x29d788 
I/DEBUG (1055): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 
I/DEBUG (1055): Build fingerprint: 'LENOVO/IdeaPad_Tablet_A1_07/A1_07:2.3.4/GRJ22/eng.user.20120209.100319:user/release-keys' 
I/DEBUG (1055): pid: 5419, tid: 5427 >>> eu.eversystems.sample <<< 
I/DEBUG (1055): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000024 
I/DEBUG (1055): r0 0029d788 r1 463ddb64 r2 5f776e64 r3 00000000 

回答

-1

U可以在清單中使用

android:noHistory="true" 

爲所有的活動:),然後

finish() 

你當前活動..你完成了! :)我希望它有幫助

1

我還沒有嘗試過這一點,但我會想象,調用本地活動指針ANativeActivity_finish()將工作。

+0

這其他的線程也似乎預示着這個問題:http://stackoverflow.com/questions/8088086/ nativeactivity-does-not-finish(我仍然會嘗試它,看看會發生什麼) – 2012-08-29 14:04:36

0

如果你看看NDK中的native-activity示例,android_main是一個無效函數,所以當你想「退出」時你只需「返回」。

但是,您應該注意如何/何時從android_main返回。在正常情況下,Android上的應用程序並不真正「結束」。它們被帶入和退出前景。當然,如果進程在後臺並且操作系統設備不再需要,它將按照您的說法終止。在這種情況下,我期望android_native_app_glue庫中的「destroyRequested」字段被設置,這應該觸發從android_main返回。這將允許您優雅地退出您的應用程序。

在所有其他情況下,您只需要您的應用程序遵守它可能會發現自己的各種狀態。

即:

/** 
* Command from main thread: the AInputQueue has changed. Upon processing 
* this command, android_app->inputQueue will be updated to the new queue 
* (or NULL). 
*/ 
APP_CMD_INPUT_CHANGED, 

/** 
* Command from main thread: a new ANativeWindow is ready for use. Upon 
* receiving this command, android_app->window will contain the new window 
* surface. 
*/ 
APP_CMD_INIT_WINDOW, 

/** 
* Command from main thread: the existing ANativeWindow needs to be 
* terminated. Upon receiving this command, android_app->window still 
* contains the existing window; after calling android_app_exec_cmd 
* it will be set to NULL. 
*/ 
APP_CMD_TERM_WINDOW, 

/** 
* Command from main thread: the current ANativeWindow has been resized. 
* Please redraw with its new size. 
*/ 
APP_CMD_WINDOW_RESIZED, 

/** 
* Command from main thread: the system needs that the current ANativeWindow 
* be redrawn. You should redraw the window before handing this to 
* android_app_exec_cmd() in order to avoid transient drawing glitches. 
*/ 
APP_CMD_WINDOW_REDRAW_NEEDED, 

/** 
* Command from main thread: the content area of the window has changed, 
* such as from the soft input window being shown or hidden. You can 
* find the new content rect in android_app::contentRect. 
*/ 
APP_CMD_CONTENT_RECT_CHANGED, 

/** 
* Command from main thread: the app's activity window has gained 
* input focus. 
*/ 
APP_CMD_GAINED_FOCUS, 

/** 
* Command from main thread: the app's activity window has lost 
* input focus. 
*/ 
APP_CMD_LOST_FOCUS, 

/** 
* Command from main thread: the current device configuration has changed. 
*/ 
APP_CMD_CONFIG_CHANGED, 

/** 
* Command from main thread: the system is running low on memory. 
* Try to reduce your memory use. 
*/ 
APP_CMD_LOW_MEMORY, 

/** 
* Command from main thread: the app's activity has been started. 
*/ 
APP_CMD_START, 

/** 
* Command from main thread: the app's activity has been resumed. 
*/ 
APP_CMD_RESUME, 

/** 
* Command from main thread: the app should generate a new saved state 
* for itself, to restore from later if needed. If you have saved state, 
* allocate it with malloc and place it in android_app.savedState with 
* the size in android_app.savedStateSize. The will be freed for you 
* later. 
*/ 
APP_CMD_SAVE_STATE, 

/** 
* Command from main thread: the app's activity has been paused. 
*/ 
APP_CMD_PAUSE, 

/** 
* Command from main thread: the app's activity has been stopped. 
*/ 
APP_CMD_STOP, 

/** 
* Command from main thread: the app's activity is being destroyed, 
* and waiting for the app thread to clean up and exit before proceeding. 
*/ 
APP_CMD_DESTROY, 

的示例代碼是在這裏:

http://developer.android.com/reference/android/app/NativeActivity.html

+0

我想知道如果我正在退出,並且最終的奇怪轉儲對於調試應用來說是正常的/預期的。 – 2012-10-09 06:48:25