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