DrawerLayout stops working when fragment back stack is popped - android

I have a DrawerLayout connected to the ActionBar with an ActionBarDrawerToggle that stops working. I know the question about why it doesn't open has been asked 100 times but my question is a little different. My drawer works! Yep, you click on the hamburger and the drawer opens and closes like a champ...
The problem is that it stops working when I dismiss any fragment that was added to the activity. Here is the activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/renderer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/activity_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
...header stuff...
<ViewStub
android:id="#+id/view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"/>
...footer stuff...
</RelativeLayout>
<FrameLayout
android:id="#+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Then the activity contains this...
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mRendererLayout = (DrawerLayout) findViewById(R.id.renderer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mRendererLayout,
R.string.alert_ok,
R.string.cancel_all
) {
#Override
public void onDrawerOpened(View drawerView) {
Log.e("===", "RA.Drawer Opened ");
}
#Override
public void onDrawerClosed(View drawerView) {
Log.e("===", "RA.Drawer Closed ");
}
};
mRendererLayout.addDrawerListener( mDrawerToggle );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("===", "RA.onOptionsItemSelected(): ");
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
At a few points I add various fragments to the activity with
addFragment( new MyFragment() );
Each fragment has a view with an onClick() that calls
getActivity().getSupportFragmentManager().popBackStack();
It is at this point that the drawer stops working. The fragment disappears as expected and it does log "RA.onOptionsItemSelected()" when the hamburger is clicked but "RA.Drawer Opened" is no longer logged and the drawer does not open. This continues until the activity is relaunched.
To make things more complicated the activity uses a ViewStub to swap out two layouts: either a FrameLayout or a FrameLayout containing a ViewPager. Only the latter has the problem. I swap out the viewStub like this...
ViewStub viewStub = findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.either_layout_here);
viewStub.inflate();
Swapping in this layout has no problems with the drawer. It gets a fragment added to it via addFragment():
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Swapping in this layout, on the other hand, has the problem...
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
This one does not get a fragment added to its content_frame (yet), since the ViewPager handles its fragments. That's really the only difference. For some reason popping the fragment back stack breaks the drawer button on the ActionBar.
I'm stumped. Help!

Related

How to add a back button to action bar / toolbar

I have written a piece of code as part of an app where I want to implement a back button on the action bar/tool bar such that when the button is pressed, the previous page (the page/fragment immediately before the current page/fragment) will be displayed.
This is the code for the ToolBar, DrawerLayout, NavigationView and getSupportActionBar():
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
I am unable to use ActionBar. For some reason (I don't know why), my Android studio/ program, will not allow me to use the ActionBar. So I am substituting that with the set/getSupportActionBar().
The function used in relation to this are:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
My activity_main.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/activity_main"
android:orientation="vertical"
tools:openDrawer="start"
tools:context="com.example.albin.settings_menu.SettingsActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginTop="-24dp"
app:menu="#menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</LinearLayout>
The problem is that I don't know which is the useful code, which is the useless code and how to mix/join/(add additional codes to) these (codes, methods, variables/objects, fragments, xml layouts) to get the desired outcome, that is, the application of a back button on the action bar/tool bar.
Most of the code above is implemented for the up button, not the back button. I have read at several places that up and back buttons are not the same.
I tried several links on internet as well as on this site, but none of them has just what I need.
Hope someone can give me an clear answer...
You can include the back icon in ToolBar:
Initialize ToolBar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You can use an drawable icon as a back button.
toolbar.setNavigationIcon(R.drawable.your_drawable_icon);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// what do you want here
}
});
If you do not want to use drawable icon then:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// what do you want here
}
});
Actually your layout having that issue because you have added toolbar in RelativeLayout so drawer layout is overlapping on it that's why you would not able to click on back arrow, i have fix your layout see below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:navigationIcon="#drawable/ic_back_black"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="Settings" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/options_menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
The simplest way would be to add parent activity in manifest file as developer docs suggest.
<activity
android:name=".ChildActivity"
android:parentActivityName=".ParentActivity" >
and java code you already have done it, setSupportActionbar and setHomeAsUpEnabled.
Edited :
its necessary to add up action for icon to be visible, as mentioned in
Android Developer Docs
So toolbar gives added flexibility to modify title-bar in Android.
As far as why getActionBar is not working and you are compelled to use getSupportActionBar is because you must be using SupportLibrary. SupportLibrary gives backward compatibility to earlier SDK versions.
If you want to modify your title-bar/header/action-bar extensively
then use toolbar otherwise use action-bar.
Add a navigation click listener to your toolbar , like below
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
If you are referencing some actions from the action bar, such as a Save action or a Share one, and you are overriding onOptionsItemSelected method, then you need to define the behavior when the back or home button is clicked:
#Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
//save stuff
break;
//this is what you need to add to reference again back/home button
case android.R.id.home:
//do your stuff here, usually back to the home or close the current activity
getActivity().finish();
break;
default:
break;
}
return true;

Android toolbar navigation back button callback never called

I know there are a lot of question like this answered, but nothing I found works.
I have an activity (MainActivity) that takes care of showing different fragments. Also, in the activity, I have navigation drawer which I show only on one fragment (it is hidden on others).
The problem is that toolbar navigation callback is never called. Back button is showed, but click on it does nothing. Also (I don't know if it makes any difference) but user get to that fragment from another one in which I do same things as in this one except navigation back button is hidden.
XML for activity is this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/main_toolbar"
layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
Included toolbar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:theme="#style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"/>
Now, in activity I setup a toolbar and put a listener on its back button:
setSupportActionBar(mainToolbar);
setTitle(getResources().getString("Some title"));
mainToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
In that specific fragment where I need back button I do this to show it:
if (this.context.getSupportActionBar() != null) {
this.context.getSupportActionBar().setTitle("Fragment title"));
this.context.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.context.getSupportActionBar().setDisplayShowHomeEnabled(true);
}
And, since I don't want navigation drawer to be used/showed, in the fragment I call a method defined in the activity that executes this code:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
drawerToggle.setDrawerIndicatorEnabled(false);
On a specific fragment where you want to show back button simply add that fragment to backstack
add this BackStack change listener to Activity
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() != 0)
mToolbar.setNavigationIcon(R.drawable.ic_action_back);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
to add to backStack you can use that line
if you pass true to addtoback stack then it will add in and backstack.
public void changeFragment(Fragment fragment, boolean addtobackstack) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit);
fragmentTransaction.replace(R.id.fragment_container, fragment, MainActivity.TAG);
if (addtobackstack)
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

Implementing Navigation Drawer Without TitleBar

I'm planning to implement a navigation bar to let users navigate to different activites.
But here's the problem,
I've found plenty articles about creating a navigation drawer but it seems doesn't work for me , because my UI doesn't have any titlebar.
And what i actually want is to call up the navigation drawer whenever users press on a button near to the navigation drawer.
Is there any possible way to do this ?
It is very simple.
Here is your main activity's layout, activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<FrameLayout
android:animateLayoutChanges="true"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_list_header"
app:menu="#menu/navigation"/>
</android.support.v4.widget.DrawerLayout>
And here's your MainActivity:
public class MainActivity extends AppCompatActivity {
NavigationView navigationView;
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navi_1:
// on 1st item in the menu, do something
break;
case R.id.navi_2:
// on 2nd item in the menu, do something
break;
}
drawerLayout.closeDrawers();
return false;
}
});
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
// [...]
private void openDrawer() {
if (!drawerLayout.isDrawerOpen(navigationView)) {
drawerLayout.openDrawer(navigationView);
}
}
private void closeDrawer() {
if (drawerLayout.isDrawerOpen(navigationView)) {
drawerLayout.closeDrawers();
}
}
}
Now you can open the drawer with openDrawer() and close it with closeDrawer().
A sample navigation.xml file which is located under the menu dir in the res (resources) folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/navi_1"
android:checked="true"
android:icon="#drawable/ic_android"
android:title="First item"/>
<item
android:id="#+id/navi_2"
android:icon="#drawable/ic_android"
android:title="Second item"/>
</group>
</menu>
Sample drawer_list_header.xml file, located under the layout dir in the res folder:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:paddingBottom="8dp"
android:src="#drawable/list_header_final">
</ImageView>
Here are some notes:
You have to declare a menu file for the drawer, see app:menu="#menu/navigation" in the layout file.
You might want to declare a header layout, which is displayed over the menu elements in the drawer, see app:headerLayout="#layout/drawer_list_header".
The drawer can be opened by a fling-like action from the edge of the screen. To prevent the users from doing that, you might want to lock/unlock your drawer on action using drawerLayout.setDrawerLockMode(...);, see the documentation for details.
Also note that in order to use NavigationView, you'll need the latest design support lib by adding the dependency to your module's gradle file: compile 'com.android.support:design:22.2.0'.
See more about it here.
Yes you can implement DrawerLayout with out ActionBar. You can manually open and close the DrawerLayout like
drawerLayout.openDrawer(Gravity.LEFT);
drawerLayout.closeDrawer(Gravity.LEFT);

android: fragment or contextual layout in Navigation Drawer

The question says it all.
I dont know if fragments can be included inside Navigation Drawer or not.
I am more interested to make contextual layout.
The Navigation drawer in Google play and YouTube app is not just a simple drawer.
I dont know if fragments can be included inside Navigation Drawer or not
Yes, they can be integrated just as in any valid part of the layout. Just put in the drawer a ViewGroup (FrameLayout, LinearLayout, etc) and instruct the FragmentManager to put a fragment in that view group by giving the layout id in add or replace method.
I am not sure what you mean by Contextual Layout, but I've checked the Google Play and Youtube apps and in their drawer layout it doesn't look impossible to have fragments inside.
EDIT: Below is some basic example with a drawer having a fragment. I guess you know about this developer article with how to customize the drawer in conjunction with the action bar.
drawer_main.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<FrameLayout
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.left_drawer, getDrawerFragment()).commit();
}
}
private Fragment getDrawerFragment() {
return new DrawerFragment();
}
}
DrawerFragment.java:
public class DrawerFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.drawer_fragment_layout, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
ListView listView = (ListView) view.findViewById(R.id.main_list_view);
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, new String[] {
"Adam", "Diana", "John"
}));
}
}
fragment's layout drawer_fragment_layout.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_list_view"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Makes sense?
You can use fragment or just a layout. Navigation drawer has to child layout. First one is main, second is drawer. These two child layout can be just a layout or a fragment like youtube ext. You can find all basic information in here.

DrawerLayout is hidden below the GridView

I try to implement the latest drawer layout from support library 13.
With the following code, the drawer is always showing below the gridview. Even I try to call bringToFront() still not working. Can help to find what's wrong? Thanks.
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
member_home_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<GridView
android:id="#+id/member_home_thumbnail_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" >
</GridView>
</LinearLayout>
The main activity:
public class BaseRootActivity extends BaseActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mMainMenus;
public ListView getDrawerListView() {
return this.mDrawerList;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainMenus = this.getResources().getStringArray(R.array.main_menu_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) this.findViewById(R.id.left_drawer);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mMainMenus));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("actionbar title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("drawer title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
...
in main fragment
public class MemberHomeFragment extends Fragment implements OnItemClickListener {
private GridView mGridListView;
private UserThumbnailAdapter memberAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.member_home_layout, container, false);
mGridListView = (GridView) rootView.findViewById(R.id.member_home_thumbnail_grid);
mGridListView.setOnItemClickListener(this);
memberAdapter = new UserThumbnailAdapter(this.getActivity(), null);
mGridListView.setAdapter(memberAdapter);
this.startLoading(); // load thumbnails
return rootView;
}
So this isn't a problem with the way you are using the drawer layout as I mentioned earlier. It's a problem with the way you are using fragments. The way the FragmentManager works is you create a fragment transaction, and you tell the fragment transaction a set piece of work and then you commit it, so that it all happens at once.
When you add, remove, or in your case Replace a fragment you have to tell the FragmentManager where to put your fragment, and you were giving it the wrong location. You were telling it to put your fragment inside android.R.id.content, a place that ALL of the content on the screen lives inside. You only want to add a fragment to one your own containers. The fact that the ID you specified (android.R.id.content) begins with 'android', is a big giveaway that it is the android systems, and not yours.
You instead want to put it in the location inside of your navigation drawer that you specified, which is R.id.content_frame. You can see how you specified that in your xml above, which I will copy here for you:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
I cannot understand but here is the issue:
As #spierce7 pointed out, the way I call fragment replacement is not correct (but why?)
My code is:
getFragmentManager().beginTransaction().replace(android.R.id.content, new MemberHomeFragment()).commit();
and when I changed to:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new MemberHomeFragment()).commit();
It works.

Categories

Resources