package fr.letroll.androidapplicationlifecycle;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        report("onCreate enter");
        super.onCreate(savedInstanceState);
        report("super.onCreate done");

        setContentView(R.layout.activity_main);
        report("setContentView done");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        getSupportFragmentManager().addOnBackStackChangedListener(new BackStackListener());

        tv = (TextView)findViewById(R.id.tv);

        AdView mAdView = (AdView) findViewById(R.id.adView);
//        AdRequest adRequest = new AdRequest.Builder().build();

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
                .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")  // My Galaxy Nexus test phone
                .build();

        mAdView.loadAd(adRequest);

        report("onCreate exit");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public MainActivity()
    {
        report("constructor");
    }

    private void report(String txt){
        Log.i(TAG,txt);
        if(tv!=null)tv.append(txt+"\n");
    }

    @Override
    protected void onDestroy()
    {
        report("onDestroy enter");
        super.onDestroy();
        report("onDestroy exit");
    }


    @Override
    protected void onPause()
    {
        report("onPause enter");
        super.onPause();
        report("onPause exit");
    }


    @Override
    protected void onResume()
    {
        report("onResume enter");
        super.onResume();
        report("onResume exit");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        report("onSaveInstanceState enter");
        super.onSaveInstanceState(outState);
        report("onSaveInstanceState exit");
    }


    @Override
    protected void onStart()
    {
        report("onStart enter");
        super.onStart();
        report("onStart exit");
    }


    @Override
    protected void onStop()
    {
        report("onStop enter");
        super.onStop();
        report("onStop exit");
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        report("onRestoreInstanceState enter");
        super.onRestoreInstanceState(savedInstanceState);
        report("onRestoreInstanceState exit");
    }

    private class BackStackListener implements FragmentManager.OnBackStackChangedListener
    {
        @Override
        public void onBackStackChanged()
        {
            report("onBackStackChanged");


            FragmentManager fm = MainActivity.this.getSupportFragmentManager();
            if (fm.getBackStackEntryCount() == 0)
            {
                report("back stack empty");
            }else report("back stack count:"+fm.getBackStackEntryCount());
        }
    }
}

@@ -1643,18 +1643,16 @@
 dView);%0A
-//
         
@@ -1674,62 +1674,52 @@
 uest
- = new AdRequest.Builder().build();%0A%0A        AdRequest
+;%0A        if(BuildConfig.DEBUG)%7B%0A           
  adR
@@ -1751,16 +1751,20 @@
 ilder()%0A
+    
         
@@ -1848,32 +1848,36 @@
                 
+   
+ 
 .addTestDevice(%22
@@ -1958,16 +1958,20 @@
         
+    
 .build()
@@ -1967,24 +1967,106 @@
    .build();
+%0A        %7Delse%7B%0A            adRequest = new AdRequest.Builder().build();%0A        %7D
 %0A%0A        mA

prepare for release