package com.bsunk.esplight;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.bsunk.esplight.addModule.AddModuleFragment;
import com.bsunk.esplight.devicesList.DeviceListFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(getString(R.string.devices_tab));

        DeviceListFragment deviceListFragment = new DeviceListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.content, deviceListFragment).commit();

        bottomNavigationView.setOnNavigationItemSelectedListener(item ->  {
                Fragment fragment;
                switch (item.getItemId()) {
                    case R.id.action_devices:
                        fragment = new DeviceListFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
                        getSupportActionBar().setTitle(getString(R.string.devices_tab));
                        break;
                    case R.id.action_add:
                        fragment = new AddModuleFragment();
                        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
                        getSupportActionBar().setTitle(getString(R.string.add_device_tab));
                        break;
                    case R.id.action_settings:
                        getSupportActionBar().setTitle(getString(R.string.settings_tab));
                        break;
                }
                return true;
        });

    }



}

@@ -481,16 +481,37 @@
 vity %7B%0A%0A
+    int selected=0;%0A%0A
     @Ove
@@ -872,32 +872,208 @@
 onBar(toolbar);%0A
+%0A        if(savedInstanceState!=null) %7B%0A            selected = savedInstanceState.getInt(%22selection%22);%0A            goToSelectedFragment(selected);%0A        %7D%0A        else %7B%0A    
         getSuppo
@@ -1129,17 +1129,20 @@
 _tab));%0A
-%0A
+    
         
@@ -1207,32 +1207,36 @@
 ment();%0A        
+  
+  
 getSupportFragme
@@ -1317,16 +1317,26 @@
 ommit();
+%0A        %7D
 %0A%0A      
@@ -1409,43 +1409,8 @@
   %7B%0A
-                Fragment fragment;%0A
     
@@ -1511,32 +1511,552 @@
                 
+selected=0;%0A                        break;%0A                    case R.id.action_add:%0A                        selected=1;%0A                        break;%0A                    case R.id.action_settings:%0A                        selected=2;%0A                        break;%0A                %7D%0A            goToSelectedFragment(selected);%0A            return true;%0A        %7D);%0A%0A    %7D%0A%0A    private void goToSelectedFragment(int selection) %7B%0A        Fragment fragment;%0A        switch (selection) %7B%0A            case 0:%0A                
 fragment = new D
@@ -2072,32 +2072,24 @@
 Fragment();%0A
-        
             
@@ -2193,32 +2193,24 @@
             
-        
 getSupportAc
@@ -2274,32 +2274,24 @@
             
- 
-       
 break;%0A     
@@ -2301,46 +2301,16 @@
     
-     
-   
 case 
-R.id.action_add:%0A        
+1:%0A
     
@@ -2353,32 +2353,24 @@
 Fragment();%0A
-        
             
@@ -2474,32 +2474,24 @@
             
-        
 getSupportAc
@@ -2562,38 +2562,23 @@
         
-        break;%0A       
+break;%0A
         
@@ -2585,44 +2585,16 @@
     
- 
 case 
-R.id.action_settings:%0A        
+2:%0A
     
@@ -2683,32 +2683,24 @@
             
-        
 break;%0A     
@@ -2706,67 +2706,121 @@
     
-    
+%7D%0A
     %7D%0A
+%0A
     
-            return true;%0A        %7D
+public void onSaveInstanceState(Bundle bundle) %7B%0A        bundle.putInt(%22selection%22, selected
 );%0A
-%0A
     %7D%0A%0A
-%0A%0A
 %7D%0A

Add saving of fragment when on configuration change