SHOW:
|
|
- or go back to the newest paste.
| 1 | ////////////////////////////////////////////// | |
| 2 | // MainActivity (simplified class) | |
| 3 | ////////////////////////////////////////////// | |
| 4 | ||
| 5 | public class MainActivity extends ActionBarActivity {
| |
| 6 | ||
| 7 | private NavigationDrawerFragment mNavigationDrawerFragment; | |
| 8 | ||
| 9 | ||
| 10 | @Override | |
| 11 | protected void onCreate(Bundle savedInstanceState) {
| |
| 12 | super.onCreate(savedInstanceState); | |
| 13 | setContentView(R.layout.activity_main); | |
| 14 | ||
| 15 | mNavigationDrawerFragment = (NavigationDrawerFragment) | |
| 16 | getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); | |
| 17 | mTitle = getTitle(); | |
| 18 | ||
| 19 | // Set up the drawer. | |
| 20 | mNavigationDrawerFragment.setUp( | |
| 21 | R.id.navigation_drawer, | |
| 22 | (DrawerLayout) findViewById(R.id.drawer_layout)); | |
| 23 | ||
| 24 | // Show content for the first menu item | |
| 25 | // when the app launches. | |
| 26 | if (savedInstanceState == null) {
| |
| 27 | showContentForNavigationDrawerItem(0); | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | @Override | |
| 32 | public boolean onOptionsItemSelected(MenuItem item) {
| |
| 33 | // ActionBarDrawerToggle drawerToggle = mNavigationDrawerFragment.getDrawerToggle(); | |
| 34 | // if (drawerToggle.isDrawerIndicatorEnabled() && | |
| 35 | // drawerToggle.onOptionsItemSelected(item)) {
| |
| 36 | // return true; | |
| 37 | // } | |
| 38 | switch (item.getItemId()) {
| |
| 39 | case android.R.id.home: | |
| 40 | FragmentManager fragmentManager = getSupportFragmentManager(); | |
| 41 | fragmentManager.popBackStack(); | |
| 42 | fragmentManager.executePendingTransactions(); | |
| 43 | mNavigationDrawerFragment.toggleDrawerIndicator(); | |
| 44 | return true; | |
| 45 | default: | |
| 46 | return super.onOptionsItemSelected(item); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | @Override | |
| 51 | public void onBackPressed() {
| |
| 52 | FragmentManager fragmentManager = getSupportFragmentManager(); | |
| 53 | fragmentManager.executePendingTransactions(); | |
| 54 | if (fragmentManager.getBackStackEntryCount() < 1) {
| |
| 55 | super.onBackPressed(); | |
| 56 | } else {
| |
| 57 | fragmentManager.executePendingTransactions(); | |
| 58 | fragmentManager.popBackStack(); | |
| 59 | fragmentManager.executePendingTransactions(); | |
| 60 | mNavigationDrawerFragment.toggleDrawerIndicator(); | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | } | |
| 65 | ||
| 66 | ////////////////////////////////////////////// | |
| 67 | // NavigationDrawerFragment (simplified class) | |
| 68 | ////////////////////////////////////////////// | |
| 69 | ||
| 70 | public class NavigationDrawerFragment extends Fragment {
| |
| 71 | ||
| 72 | private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; | |
| 73 | ||
| 74 | private ActionBarDrawerToggle mDrawerToggle; | |
| 75 | ||
| 76 | private DrawerLayout mDrawerLayout; | |
| 77 | private ListView mDrawerListView; | |
| 78 | private View mFragmentContainerView; | |
| 79 | ||
| 80 | private int mCurrentSelectedPosition = 0; | |
| 81 | private boolean mFromSavedInstanceState; | |
| 82 | private boolean mUserLearnedDrawer; | |
| 83 | ||
| 84 | public NavigationDrawerFragment() {
| |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public void onCreate(Bundle savedInstanceState) {
| |
| 89 | super.onCreate(savedInstanceState); | |
| 90 | - | Injector.inject(this); |
| 90 | + | |
| 91 | getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
| |
| 92 | @Override | |
| 93 | public void onBackStackChanged() {
| |
| 94 | toggleDrawerIndicator(); | |
| 95 | } | |
| 96 | }); | |
| 97 | mUserLearnedDrawer = mPreferenceHelper.restoreUserLearnedDrawer(); | |
| 98 | ||
| 99 | if (savedInstanceState != null) {
| |
| 100 | mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); | |
| 101 | mFromSavedInstanceState = true; | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | public void toggleDrawerIndicator() {
| |
| 106 | int backStackCount = getFragmentManager().getBackStackEntryCount(); | |
| 107 | boolean enableIndicator = backStackCount == 0; | |
| 108 | mDrawerToggle.setDrawerIndicatorEnabled(enableIndicator); | |
| 109 | } | |
| 110 | ||
| 111 | @Override | |
| 112 | public void onActivityCreated(Bundle savedInstanceState) {
| |
| 113 | super.onActivityCreated(savedInstanceState); | |
| 114 | setHasOptionsMenu(true); | |
| 115 | } | |
| 116 | ||
| 117 | public boolean isDrawerOpen() {
| |
| 118 | return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); | |
| 119 | } | |
| 120 | ||
| 121 | public void setUp(int fragmentId, DrawerLayout drawerLayout) {
| |
| 122 | mFragmentContainerView = getActivity().findViewById(fragmentId); | |
| 123 | mDrawerLayout = drawerLayout; | |
| 124 | mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); | |
| 125 | ||
| 126 | ActionBar actionBar = getActionBar(); | |
| 127 | actionBar.setDisplayHomeAsUpEnabled(true); | |
| 128 | actionBar.setHomeButtonEnabled(true); | |
| 129 | ||
| 130 | mDrawerToggle = new ActionBarDrawerToggle( | |
| 131 | getActivity(), /* host Activity */ | |
| 132 | mDrawerLayout, /* DrawerLayout object */ | |
| 133 | R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ | |
| 134 | R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ | |
| 135 | R.string.navigation_drawer_close /* "close drawer" description for accessibility */ | |
| 136 | ) {
| |
| 137 | @Override | |
| 138 | public void onDrawerClosed(View drawerView) {
| |
| 139 | super.onDrawerClosed(drawerView); | |
| 140 | if (!isAdded()) {
| |
| 141 | return; | |
| 142 | } | |
| 143 | toggleDrawerIndicator(); | |
| 144 | getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu() | |
| 145 | } | |
| 146 | ||
| 147 | @Override | |
| 148 | public void onDrawerOpened(View drawerView) {
| |
| 149 | super.onDrawerOpened(drawerView); | |
| 150 | if (!isAdded()) {
| |
| 151 | return; | |
| 152 | } | |
| 153 | mDrawerToggle.setDrawerIndicatorEnabled(true); | |
| 154 | if (!mUserLearnedDrawer) {
| |
| 155 | // The user manually opened the drawer; | |
| 156 | // store this flag to prevent auto-showing | |
| 157 | // the navigation drawer automatically in the future. | |
| 158 | mUserLearnedDrawer = true; | |
| 159 | mPreferenceHelper.storeUserLearnedDrawer(true); | |
| 160 | } | |
| 161 | getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu() | |
| 162 | } | |
| 163 | }; | |
| 164 | ||
| 165 | // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, | |
| 166 | // per the navigation drawer design guidelines. | |
| 167 | if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
| |
| 168 | mDrawerLayout.openDrawer(mFragmentContainerView); | |
| 169 | } | |
| 170 | ||
| 171 | // Defer code dependent on restoration of previous instance state. | |
| 172 | mDrawerLayout.post(new Runnable() {
| |
| 173 | @Override | |
| 174 | public void run() {
| |
| 175 | mDrawerToggle.syncState(); | |
| 176 | } | |
| 177 | }); | |
| 178 | ||
| 179 | mDrawerLayout.setDrawerListener(mDrawerToggle); | |
| 180 | } | |
| 181 | ||
| 182 | } |