I have some issues switching navigation drawer functionality to up functionality at lower level fragment. I have read this thread to manage to show the up indicator. But when pressing the button, it'll open up the navigation drawer instead of going back to previous fragment. And I can't set action bar title in EditUserFragment to "Edit Profile". I'm using the navigation drawer template available in Android Studio.
I have three levels:
- MainActivity with navigation drawer that consists of Home and Profile items
- UserFragment titled with "Profile" that has an option item that'll bring up EditUserFragment
- EditUserFragment (lower level fragment) is triggered by UserFragment
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_launcher, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
}
@Override
public void onBackPressed() {
super.onBackPressed();
// this won't change the drawer indicator back
drawerToggle.setDrawerIndicatorEnabled(true);
// this works
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
public void onSectionAttached(int number) {
// Show the corresponding title on the action bar when clicked and open corresponding
// fragments.
Fragment fragment = null;
switch (number) {
case 1:
mTitle = getString(R.string.title_home);
break;
case 2:
mTitle = getString(R.string.title_profile);
fragment = new UserFragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(mTitle);
}
public void setActionBarTitle(String title){
getActionBar().setTitle(title);
Log.d("Title 2", getActionBar().getTitle().toString());
}
onBackPressed
is working partially (commented in code) when I pressed back button on phone (not up button on action bar).
UserFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false);
drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
R.drawable.ic_launcher, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
return view;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_edit:
drawerToggle.setDrawerIndicatorEnabled(false);
// Disable sliding from edge to open drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
getFragmentManager().beginTransaction()
.replace(R.id.container, new EditUserFragment())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EditUserFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).setActionBarTitle("Edit Profile");
Log.d("Title", getActivity().getActionBar().getTitle().toString());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
Log.d("KEY: ", String.valueOf(item.getItemId()));
switch (item.getItemId()) {
case android.R.id.home:
Log.d("EditUserFragment", "I'm here");
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
works as expected that up caret is showing instead of drawer; though when tapping it, it's opening up drawer instead of going back to previous fragment. In addition, the code under case android.R.id.home
is never executed.
And I try to set action bar title to "Edit Profile" from "Profile". The log shows me "Edit Profile", but the actual running app shows me "Profile" for some reason.
What I want to achieve is be able to go back to UserFragment
from EditUserFragment
by tapping the up indicator (right now the up indicator is opening up navigation drawer). And show correct title in EditUserFragment
.
Any help is greatly appreciated!!