|
| 1 | +package org.neotech.app.retainabletasksdemo; |
| 2 | + |
| 3 | +import android.content.res.Configuration; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.design.widget.NavigationView; |
| 6 | +import android.support.v4.app.Fragment; |
| 7 | +import android.support.v4.app.FragmentManager; |
| 8 | +import android.support.v4.app.FragmentTransaction; |
| 9 | +import android.support.v4.view.GravityCompat; |
| 10 | +import android.support.v4.widget.DrawerLayout; |
| 11 | +import android.support.v7.app.ActionBarDrawerToggle; |
| 12 | +import android.support.v7.app.AppCompatActivity; |
| 13 | +import android.util.Log; |
| 14 | +import android.view.MenuItem; |
| 15 | +import android.view.View; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | + |
| 19 | +public class ActivityWithFragments extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { |
| 20 | + |
| 21 | + private static final String[] FRAGMENT_TAGS = new String[]{"Fragment 1", "Fragment 2", "Fragment 3"}; |
| 22 | + |
| 23 | + private FragmentAdapter adapter; |
| 24 | + private ActionBarDrawerToggle toggle; |
| 25 | + |
| 26 | + @Override |
| 27 | + protected void onCreate(Bundle savedInstanceState) { |
| 28 | + super.onCreate(savedInstanceState); |
| 29 | + setContentView(R.layout.activity_with_fragments); |
| 30 | + |
| 31 | + adapter = new FragmentAdapter(getSupportFragmentManager(), R.id.fragment_container, 3); |
| 32 | + for (String FRAGMENT_TAG : FRAGMENT_TAGS) { |
| 33 | + adapter.addFragment(FRAGMENT_TAG, TestFragment.class); |
| 34 | + } |
| 35 | + |
| 36 | + DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 37 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 38 | + toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close); |
| 39 | + drawerLayout.addDrawerListener(toggle); |
| 40 | + NavigationView drawer = (NavigationView) findViewById(R.id.nav_view); |
| 41 | + drawer.setNavigationItemSelectedListener(this); |
| 42 | + drawer.getHeaderView(0).setVisibility(View.GONE); |
| 43 | + |
| 44 | + if(savedInstanceState == null){ |
| 45 | + adapter.setCurrentFragment(FRAGMENT_TAGS[0]); |
| 46 | + drawer.setCheckedItem(R.id.nav_fragment_1); |
| 47 | + setTitle(FRAGMENT_TAGS[0]); |
| 48 | + } else { |
| 49 | + setTitle(adapter.getCurrentFragment().getTag()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void setTitle(String title){ |
| 54 | + getSupportActionBar().setTitle(title); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void onPostCreate(Bundle savedInstanceState) { |
| 59 | + super.onPostCreate(savedInstanceState); |
| 60 | + toggle.syncState(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onBackPressed() { |
| 65 | + DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 66 | + if (drawer.isDrawerOpen(GravityCompat.START)) { |
| 67 | + drawer.closeDrawer(GravityCompat.START); |
| 68 | + } else { |
| 69 | + super.onBackPressed(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onConfigurationChanged(Configuration newConfig) { |
| 75 | + super.onConfigurationChanged(newConfig); |
| 76 | + toggle.onConfigurationChanged(newConfig); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 81 | + final int id = item.getItemId(); |
| 82 | + |
| 83 | + if (id == android.R.id.home) { |
| 84 | + final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 85 | + final NavigationView navigation = (NavigationView) findViewById(R.id.nav_view); |
| 86 | + if (drawer.isDrawerOpen(navigation)) { |
| 87 | + drawer.closeDrawer(navigation); |
| 88 | + } else { |
| 89 | + drawer.openDrawer(navigation); |
| 90 | + } |
| 91 | + return true; |
| 92 | + } |
| 93 | + return super.onOptionsItemSelected(item); |
| 94 | + } |
| 95 | + |
| 96 | + @SuppressWarnings("StatementWithEmptyBody") |
| 97 | + @Override |
| 98 | + public boolean onNavigationItemSelected(MenuItem item) { |
| 99 | + // Handle navigation view item clicks here. |
| 100 | + int id = item.getItemId(); |
| 101 | + |
| 102 | + if (id == R.id.nav_fragment_1) { |
| 103 | + adapter.setCurrentFragment(FRAGMENT_TAGS[0]); |
| 104 | + setTitle(FRAGMENT_TAGS[0]); |
| 105 | + } else if (id == R.id.nav_fragment_2) { |
| 106 | + adapter.setCurrentFragment(FRAGMENT_TAGS[1]); |
| 107 | + setTitle(FRAGMENT_TAGS[1]); |
| 108 | + } else if (id == R.id.nav_fragment_3) { |
| 109 | + adapter.setCurrentFragment(FRAGMENT_TAGS[2]); |
| 110 | + setTitle(FRAGMENT_TAGS[2]); |
| 111 | + } |
| 112 | + |
| 113 | + DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 114 | + drawer.closeDrawer(GravityCompat.START); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void onStart() { |
| 120 | + super.onStart(); |
| 121 | + } |
| 122 | + |
| 123 | + public static class FragmentAdapter { |
| 124 | + |
| 125 | + private final HashMap<String, Class<? extends Fragment>> fragments; |
| 126 | + |
| 127 | + private final FragmentManager fragmentManager; |
| 128 | + private final int containerId; |
| 129 | + |
| 130 | + public FragmentAdapter(FragmentManager fragmentManager, int containerViewId){ |
| 131 | + this(fragmentManager, containerViewId, 4); |
| 132 | + } |
| 133 | + |
| 134 | + public FragmentAdapter(FragmentManager fragmentManager, int containerViewId, int initialSize){ |
| 135 | + this.fragments = new HashMap<>(initialSize); |
| 136 | + this.fragmentManager = fragmentManager; |
| 137 | + this.containerId = containerViewId; |
| 138 | + } |
| 139 | + |
| 140 | + public void addFragment(String tag, Class<? extends Fragment> fragmentClass){ |
| 141 | + fragments.put(tag, fragmentClass); |
| 142 | + } |
| 143 | + |
| 144 | + public int size(){ |
| 145 | + return fragments.size(); |
| 146 | + } |
| 147 | + |
| 148 | + public Fragment getFragment(String tag){ |
| 149 | + return fragmentManager.findFragmentByTag(tag); |
| 150 | + } |
| 151 | + |
| 152 | + public Fragment getCurrentFragment(){ |
| 153 | + return fragmentManager.findFragmentById(containerId); |
| 154 | + } |
| 155 | + |
| 156 | + public Fragment setCurrentFragment(String tag){ |
| 157 | + //Retrieve the existing fragment or create a new one. |
| 158 | + Fragment fragment = fragmentManager.findFragmentByTag(tag); |
| 159 | + if(fragment == null) { |
| 160 | + fragment = instantiate(fragments.get(tag)); |
| 161 | + } |
| 162 | + |
| 163 | + //Check if the currently visible fragment is not equal to the fragment we're about to show. |
| 164 | + if(fragment.equals(fragmentManager.findFragmentById(containerId))){ |
| 165 | + Log.i("FragmentAdapter", "Fragment '" + tag + "' not shown because it's already visible."); |
| 166 | + return fragment; |
| 167 | + } |
| 168 | + |
| 169 | + //Show the new fragment. |
| 170 | + fragmentManager.beginTransaction() |
| 171 | + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) |
| 172 | + .replace(containerId, fragment, tag) |
| 173 | + .commit(); |
| 174 | + return fragment; |
| 175 | + } |
| 176 | + |
| 177 | + private static Fragment instantiate(Class<? extends Fragment> classType) { |
| 178 | + try { |
| 179 | + return classType.newInstance(); |
| 180 | + } catch(Exception e) { |
| 181 | + throw new Fragment.InstantiationException("Unable to instantiate fragment " + classType.getName() + ": make sure class name exists, is public, and has an empty constructor that is public", e); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments