androidc++cnative-codenative-activity

Handle main commands in android native activity


In android native activity you can handle main commands from system in your own method. I know how to do this, but i dont know what all states i can handle. I know few states like APP_CMD_SAVE_STATE, APP_CMD_INIT_WINDOW... but that´s not all. I cant find list of states or android_native_app_glue.h header file anywhere. Can someone refer me or write list of events with their numebers and when they´r called?
Thanks

void CMDMethod(struct android_app* app, int32_t state)
{
   switch(state)
   {
     case APP_CMD_SAVE_STANE: //some code
                              break;
     //what can be next cases?
   }
}

void android_main(struct android_app* state)
{
   state->onAppCmd = CMDMethod;
}

Solution

  • Heres the enum found in android_native_app_glue.h

    enum {
    
        APP_CMD_INPUT_CHANGED,
    
        APP_CMD_INIT_WINDOW,
    
        APP_CMD_TERM_WINDOW,
    
        APP_CMD_WINDOW_RESIZED,
    
        APP_CMD_WINDOW_REDRAW_NEEDED,
    
        APP_CMD_CONTENT_RECT_CHANGED,
    
        APP_CMD_GAINED_FOCUS,
    
        APP_CMD_LOST_FOCUS,
    
        APP_CMD_CONFIG_CHANGED,
    
        APP_CMD_LOW_MEMORY,
    
        APP_CMD_START,
    
        APP_CMD_RESUME,
    
        APP_CMD_SAVE_STATE,
    
        APP_CMD_PAUSE,
    
        APP_CMD_STOP,
    
        APP_CMD_DESTROY,
    
    };
    

    APP_CMD_INPUT_CHANGED - 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_INIT_WINDOW - 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_TERM_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_WINDOW_RESIZED - Command from main thread: the current ANativeWindow has been resized. Please redraw with its new size.

    APP_CMD_WINDOW_REDRAW_NEEDED - 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_CONTENT_RECT_CHANGED - 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_GAINED_FOCUS - Command from main thread: the app's activity window has gained input focus.

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

    APP_CMD_CONFIG_CHANGED - Command from main thread: the current device configuration has changed.

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

    APP_CMD_START - Command from main thread: the app's activity has been started.

    APP_CMD_RESUME - Command from main thread: the app's activity has been resumed.

    APP_CMD_SAVE_STATE - 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_PAUSE - Command from main thread: the app's activity has been paused.

    APP_CMD_STOP - Command from main thread: the app's activity has been stopped.

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