From: Mikko Rasa Date: Sun, 12 Oct 2014 15:27:43 +0000 (+0300) Subject: Deal with the activity being recreated during process lifetime X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=53ce154a2e446e82815034cb65a81b6784a767f5;p=libs%2Fcore.git Deal with the activity being recreated during process lifetime This can happen if e.g. screen orientation changes. --- diff --git a/source/core/android/main.cpp b/source/core/android/main.cpp index 2101563..a71d92f 100644 --- a/source/core/android/main.cpp +++ b/source/core/android/main.cpp @@ -2,6 +2,10 @@ extern "C" void ANativeActivity_onCreate(ANativeActivity *activity, void * /*saved_state*/, size_t /*state_size*/) { - Msp::Android::MainThread *thread = new Msp::Android::MainThread(activity); + static Msp::Android::MainThread *thread = 0; + if(thread) + thread->attach_to_activity(activity); + else + thread = new Msp::Android::MainThread(activity); thread->wait_for_app_created(); } diff --git a/source/core/android/mainthread.cpp b/source/core/android/mainthread.cpp index e07c837..1e5fc11 100644 --- a/source/core/android/mainthread.cpp +++ b/source/core/android/mainthread.cpp @@ -10,19 +10,23 @@ namespace Msp { namespace Android { MainThread::MainThread(ANativeActivity *a): - activity(a), starting_up(true) { + attach_to_activity(a); + startup_mutex.lock(); + launch(); +} + +void MainThread::attach_to_activity(ANativeActivity *a) +{ + activity = a; + activity->callbacks->onDestroy = &activity_destroyed; activity->callbacks->onNativeWindowCreated = &native_window_created; activity->callbacks->onNativeWindowResized = &native_window_resized; activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed; activity->callbacks->onInputQueueCreated = &input_queue_created; activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed; activity->instance = this; - - startup_mutex.lock(); - - launch(); } void MainThread::wait_for_app_created() @@ -48,7 +52,8 @@ void MainThread::main() Msp::Android::ErrorLogger err_logger; Msp::Application::run(1, argv, this, &app_created); free(appname); - ANativeActivity_finish(activity); + if(activity) + ANativeActivity_finish(activity); } void MainThread::app_created(void *data) @@ -56,6 +61,11 @@ void MainThread::app_created(void *data) reinterpret_cast(data)->resume_startup(); } +void MainThread::activity_destroyed(ANativeActivity *activity) +{ + reinterpret_cast(activity->instance)->activity = 0; +} + void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window) { reinterpret_cast(activity->instance)->signal_native_window_created.emit(window); diff --git a/source/core/android/mainthread.h b/source/core/android/mainthread.h index 0330352..68da6c4 100644 --- a/source/core/android/mainthread.h +++ b/source/core/android/mainthread.h @@ -26,6 +26,7 @@ private: public: MainThread(ANativeActivity *); + void attach_to_activity(ANativeActivity *); bool is_starting_up() const { return starting_up; } void wait_for_app_created(); void resume_startup(); @@ -34,6 +35,7 @@ private: virtual void main(); static void app_created(void *); + static void activity_destroyed(ANativeActivity *); static void native_window_created(ANativeActivity *, ANativeWindow *); static void native_window_resized(ANativeActivity *, ANativeWindow *); static void native_window_destroyed(ANativeActivity *, ANativeWindow *);