package ru.dyatel.tsuschedule;

import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import ru.dyatel.tsuschedule.data.DataFragment;
import ru.dyatel.tsuschedule.fragments.MainFragment;
import ru.dyatel.tsuschedule.layout.NavigationDrawerHandler;

public class MainActivity extends AppCompatActivity {

    public static final String PREFERENCES_FILE = "prefs";
    private static final String GROUP_INDEX_KEY = "group_index";
    private static final String SUBGROUP_KEY = "subgroup";

    private DataFragment dataFragment;

    private NavigationDrawerHandler drawerHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Load saved preferences
        SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
        String groupIndex = preferences.getString(GROUP_INDEX_KEY, "");
        int subgroup = preferences.getInt(SUBGROUP_KEY, 1);

        FragmentManager fragmentManager = getSupportFragmentManager();

        // Get the data fragment
        dataFragment = (DataFragment) fragmentManager.findFragmentByTag(DataFragment.TAG);
        if (dataFragment == null) {
            dataFragment = DataFragment.newInstance(groupIndex, subgroup);
            fragmentManager.beginTransaction()
                    .add(dataFragment, DataFragment.TAG)
                    .commit();
        }

        // Replace ActionBar with Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerHandler = new NavigationDrawerHandler(
                this, fragmentManager, dataFragment,
                (DrawerLayout) findViewById(R.id.drawer_layout)
        );

        fragmentManager.beginTransaction()
                .replace(R.id.content_fragment, new MainFragment())
                .commit();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerHandler.syncState();
    }

    @Override
    public void onBackPressed() {
        if (!getFragmentManager().popBackStackImmediate()) super.onBackPressed();
    }

    @Override
    public void onPause() {
        super.onPause();

        SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
        preferences.edit()
                .putString(GROUP_INDEX_KEY, dataFragment.getGroup())
                .putInt(SUBGROUP_KEY, dataFragment.getSubgroup())
                .apply();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerHandler.onConfigurationChanged(newConfig);
    }

}

@@ -229,16 +229,57 @@
 Layout;%0A
+import android.support.v7.app.ActionBar;%0A
 import a
@@ -319,16 +319,16 @@
 tivity;%0A
-
 import a
@@ -357,24 +357,54 @@
 et.Toolbar;%0A
+import android.view.MenuItem;%0A
 import ru.dy
@@ -833,16 +833,65 @@
 gment;%0A%0A
+    private NavigationHandler navigationHandler;%0A
     priv
@@ -1955,16 +1955,222 @@
 oolbar);
+%0A        ActionBar actionBar = getSupportActionBar();%0A        if (actionBar != null) %7B%0A            actionBar.setDisplayShowHomeEnabled(true);%0A            actionBar.setDisplayHomeAsUpEnabled(true);%0A        %7D
 %0A%0A      
@@ -2341,24 +2341,182 @@
         );%0A%0A
+        navigationHandler = new NavigationHandler(fragmentManager, drawerHandler);%0A        fragmentManager.addOnBackStackChangedListener(navigationHandler);%0A%0A
         frag
@@ -2878,74 +2878,257 @@
 f (!
-getFragmentManager().popBackStackImmediate()) super.onBackPressed(
+navigationHandler.onBackPressed()) super.onBackPressed();%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        return navigationHandler.onOptionsItemSelected(item)%0A                %7C%7C super.onOptionsItemSelected(item
 );%0A 

Connect NavigationHandler to the activity