I have one activity that uses two fragments, MainFragment
and DetailFragment
. The MainFragment has an expandable list view that has company names as groups and location names as children. When the user selects a category from the navigation drawer it updates the data in the adapter and updates the expandable list, displaying the correct companies.
When the user selects a location it opens the DetailFragment with information about that location and employees. The problem I'm having is when I step back. When I hit the back button it resets to the default category, and I've figured out that it's because it's calling the onViewCreated()
method again, which is where I set up my views and adapter. The strange thing is, it remembers what list items were expanded, by index. So if I had the third item expanded in a different category, the third item in the default category is still expanded.
What I'd like to know is, how can I have it maintain the setup I had when I press back?
My MainFragment
public class MainFragment extends Fragment implements ExpandableListView.OnChildClickListener, NavigationSelectedListener {
private CompaniesExpandableListAdapter adapter;
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
connectViews(view);
}
private void connectViews(View view) {
final ExpandableListView companyListView = (ExpandableListView) view.findViewById(R.id.companyListView);
final Navigation navigationItem = ((MainActivity) getActivity()).getNavigationItems().first();
getActivity().setTitle(navigationItem.getTitle());
adapter = new CompaniesExpandableListAdapter(navigationItem.getCompanies());
companyListView.setAdapter(adapter);
companyListView.setOnChildClickListener(this);
}
// ExpandableListView.OnChildClickListener
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
final DetailFragment detailFragment = new DetailFragment();
final Bundle args = new Bundle();
args.putInt(getString(R.string.extra_location_key), ((Locations) adapter.getChild(groupPosition, childPosition)).getPrimaryKey());
detailFragment.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.contentFrame, detailFragment, getString(R.string.tag_fragment_detail))
.addToBackStack(null)
.commit();
return false;
}
// NavigationSelectedListener
@Override
public void onNavigationItemSelected(Navigation navigationItem) {
// Go back to the main fragment.
getFragmentManager().popBackStack(getString(R.string.tag_fragment_main), FragmentManager.POP_BACK_STACK_INCLUSIVE);
// Send the selected item to the adapter.
adapter.updateData(navigationItem.getCompanies());
getActivity().setTitle(navigationItem.getTitle());
}
}
And my MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private NavigationSelectedListener navigationCallback;
private DrawerLayout drawerLayout;
private RealmResults<Navigation> navigationItems;
private MainFragment mainFragment = new MainFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildNavigationDrawer();
connectViews();
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) drawerLayout.closeDrawer(GravityCompat.START);
else super.onBackPressed();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navigationCallback.onNavigationItemSelected(navigationItems.get(item.getItemId()));
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void connectViews() {
navigationCallback = mainFragment;
getFragmentManager()
.beginTransaction()
.replace(R.id.contentFrame, mainFragment, getString(R.string.tag_fragment_main))
.commit();
}
private void buildNavigationDrawer() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
// Hides the device keyboard when the navigation drawer is opened.
if (getCurrentFocus() != null) ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
};
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
fillNavigationDrawer();
}
public RealmResults<Navigation> getNavigationItems() {
return navigationItems;
}
}