I want to use a single activity called MainActivity which has a drawer layout and main content container(using FrameLayout). The drawer layout for now is just a normal ListView which has only two items for now. I have made two Fragments for each of those items. The first item is Home and the second is Saved. When the MainActivity onCreate() is called I replace the container with the HomeFragment and from there I can use the Drawer to Navigate to the SavedFragment. The Navigation works perfectly and has no issues. The task which I want to achieve is that whenever I go to SavedFragment by clicking the second item on the Drawer, the Hamburger icon should change to back button/up button. I managed to change it by going through a lot of StackOverflow topics on the same issue, but I am not able to add click event on that. It doesn't listen for a click. I read some very good stuff on StackOverflow and expected that it would work for me as well, but maybe I am wrong somewhere in my code. So I am sharing the code over here:
MainActivity (Only Activity in the application) Code
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView listView;
Fragment mFragment;
ArrayList<NavigationDataRecord> drawerRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
listView = (ListView) findViewById(R.id.drawer_left);
listView.setOnItemClickListener(this);
drawerRecord = new ArrayList<NavigationDataRecord>();
drawerRecord.add(new NavigationDataRecord(R.mipmap.ic_home_grey600_24dp, "Home"));
drawerRecord.add(new NavigationDataRecord(R.mipmap.ic_save_grey600_24dp, "Saved"));
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_row, drawerRecord);
listView.setAdapter(adapter);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.app_name, R.string.app_name) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
mFragment = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, mFragment).commit();
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
mFragment = new HomeFragment();
break;
case 1:
mFragment = new SavedFragment();
break;
default:
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container,mFragment).addToBackStack(null).commit();
mDrawerLayout.closeDrawer(listView);
}}
SavedFragment Code
public class SavedFragment extends Fragment {
List<SavedInformation> list;
RecyclerView cardView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.saved_fragment, container, false);
list = new ArrayList<SavedInformation>();
makeCardList();
cardView = (RecyclerView) view.findViewById(R.id.cardList);
//cardView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
cardView.setLayoutManager(llm);
SavedInformationAdapter adapter = new SavedInformationAdapter(list, getActivity());
cardView.setAdapter(adapter);
return view;
}
private void makeCardList() {
DatabaseAdapter adapter = new DatabaseAdapter(getActivity());
Cursor cursor = adapter.getData();
while (cursor.moveToNext()) {
SavedInformation information = new SavedInformation();
information.setUsername(cursor.getString(0));
information.setDomainName(cursor.getString(1));
information.setImageURL(cursor.getString(2));
list.add(information);
}
}}
I tried a few things on these, but had no luck so I am posting the clean version of the code on which I tried implementing solutions.
I am sure that those solutions were correct, but I am unable to understand Why those did not work for me.
I applied the solution given here: Switching between Android Navigation Drawer image and Up caret when using fragments