package com.github.ayltai.newspaper;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.github.ayltai.newspaper.util.TestUtils;

import jp.wasabeef.takt.Seat;
import jp.wasabeef.takt.Takt;

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!TestUtils.isRunningInstrumentalTest()) Takt.stock(this.getApplication()).seat(Seat.TOP_RIGHT).color(Color.WHITE).play();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (!TestUtils.isRunningInstrumentalTest()) {
            try {
                Takt.finish();
            } catch (final IllegalArgumentException e) {
                // Ignored
            }
        }
    }
}

@@ -354,24 +354,153 @@
 tActivity %7B%0A
+    //region Variables%0A%0A    private static int     referenceCount;%0A    private static boolean pendingCreation;%0A%0A    //endregion%0A%0A
     @Overrid
@@ -675,89 +675,234 @@
 ()) 
-Takt.stock(this.getApplication()).seat(Seat.TOP_RIGHT).color(Color.WHITE).play();
+%7B%0A            if (BaseActivity.referenceCount %3E 0) %7B%0A                BaseActivity.stopTakt();%0A%0A                BaseActivity.pendingCreation = true;%0A            %7D else %7B%0A                this.startTakt();%0A            %7D%0A        %7D
 %0A   
@@ -956,36 +956,8 @@
 ) %7B%0A
-        super.onDestroy();%0A%0A
     
@@ -1022,49 +1022,500 @@
     
-try %7B%0A                Takt.finish
+if (BaseActivity.pendingCreation) %7B%0A                BaseActivity.pendingCreation = false;%0A%0A                this.startTakt();%0A            %7D else %7B%0A                BaseActivity.stopTakt();%0A            %7D%0A        %7D%0A%0A        super.onDestroy();%0A    %7D%0A%0A    private void startTakt() %7B%0A        Takt.stock(this.getApplication()).seat(Seat.TOP_RIGHT).color(Color.WHITE).play
 ();%0A
+%0A
     
+    BaseActivity.referenceCount++;%0A    %7D%0A%0A    private static void stopTakt() %7B%0A        try %7B%0A            Takt.finish();%0A
     
@@ -1558,25 +1558,44 @@
 ception 
+%7C NullPointerException 
 e) %7B%0A
-    
         
@@ -1609,37 +1609,34 @@
 Ignored%0A        
-    
 %7D
+%0A
 %0A        %7D%0A    %7D
@@ -1624,22 +1624,51 @@
  %7D%0A%0A        
-%7D
+BaseActivity.referenceCount--;
 %0A    %7D%0A%7D%0A

Handle Activity life-cycle properly to recreate Takt