Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – handling Activity events


We are not done yet. Let's continue our example to handle activity events and log them to the LogCat view:

  1. Continue editing jni/EventLoop.cpp. Implement activate() and deactivate().Check both activity states before notifying the listener (to avoid untimely triggering). We consider an activity as activated only if a display window is available:

    ...
    void EventLoop::activate() {
        // Enables activity only if a window is available.
        if ((!mEnabled) && (mApplication->window != NULL)) {
            mQuit = false; mEnabled = true;
            if (mActivityHandler.onActivate() != STATUS_OK) {
                goto ERROR;
            }
        }
        return;
    
    ERROR:
        mQuit = true;
        deactivate();
        ANativeActivity_finish(mApplication->activity);
    }
    
    void EventLoop::deactivate() {
        if (mEnabled) {
            mActivityHandler.onDeactivate();
            mEnabled = false;
        }
    }
    ...
    • Route activity events from the static callback callback_appEvent() to the member...