Deal with the activity being recreated during process lifetime
authorMikko Rasa <tdb@tdb.fi>
Sun, 12 Oct 2014 15:27:43 +0000 (18:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 12 Oct 2014 15:27:43 +0000 (18:27 +0300)
This can happen if e.g. screen orientation changes.

source/core/android/main.cpp
source/core/android/mainthread.cpp
source/core/android/mainthread.h

index 2101563b3f1a91a4c5253670ec6623e6ec98c389..a71d92f1c279d0ba43c24c62ea656024d726cdca 100644 (file)
@@ -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();
 }
index e07c837bcfdb15706f33e62baa98eeb1b82b26db..1e5fc1198072ef48cc84c48fead2436bcba3e39b 100644 (file)
@@ -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<MainThread *>(data)->resume_startup();
 }
 
+void MainThread::activity_destroyed(ANativeActivity *activity)
+{
+       reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
+}
+
 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
 {
        reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
index 03303523748a1f96553254875c775a346414df6a..68da6c4bc9133de65366f1a9ebe2bf72518e804b 100644 (file)
@@ -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 *);