I have a Activity A, a ListFragment P and 2 Fragments Q and R.
When the app is launched, A is created, which loads P. Based on what user clicks, it is replaced by Q or R.
Now by referencing this tutorial, I have implemented a Navigation Drawer which shows certain items to the user. However, since I have implemented the Navigation Drawer in the Activity, it shows for all the Fragments.
I want it to be only available to P.
(Very much similar to Googles Gmail app. When the user is on the main screen - the drawer is present. When user taps to open an email, the drawer changes to back button)
I am not sure how to translate the above code. Any help is appreciated.
I solved this issue by simply overriding the up carat behavior by calling the mDrawerToggle.setDrawerIndicatorEnabled(enable) and passing the boolean enable or disable as needed.
(The fragments where I didn't want the drawer to show called this method with false and where I wanted the drawer to be shown called this method with true. I put the call inside the onResume() of the respective fragment for obvious reasons.)
This works exactly like I want, and I did not have to change the design of my project :).
What you can do is create a new FragmanetActivity S and replace Q and R accordingly. If you are app is for Android 3.0 lower user ActionBarActivity create a actionbar and set its setDisplayHomeAsUpEnabled(true). As the new FragmentActivity will have new layout there will be no NavigationDrawer.
Related
I have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html
I found a lot of informations about the setRetainInstance method, but I'd like to know how to implement this method in a Navigation Drawer Activity?
I basically have:
An Activity, that contains...
a NavigationDrawerFragment, that contains...
a TopLevelMapFragment (showing Google Maps).
My aim is to prevent a reload of the whole maps and its tracks when the user rotates the screen. What is the basic concept of setRetainingInstance in an Navigation Drawer Activity? Do I have to call setRetainingInstance(true) in the NavigationDrawerFragment AND the TopLevelMapFragment?
I finally found the solution to my Problem:
When I created the Navigation Drawer Activity, I relied on the default Navigation Drawer Activity-template of Android Studio. In this template, the FragmentTransaction-replace method is called each time the user clicks on an item in the Navigation Drawer. Problem: the replace-method destroys all the fragments that are in the specified container.
So if you want to keep your fragments alive, don't ever use the replace method. You have to use the methods add, show and hide instead.
In addition, I call the setRetainInstance(true)-method in my Google Maps-Fragment, so that the fragment stays alive when the user rotates the screen for example.
I am developing an Android project with Android Studio.
I would like to have the menu is always accessible in every view like Google Play Store (the Sidebar) and App Store (the Bottom selection bar).
I am thinking to do it in two ways:
Make my app have only one activity with Navigation Drawer, all the other views are above this activity using fragments.
Recreate the sidebar or bottom selection bar every time I switch to another activity.
Both of these two ways are very complex and cost a lot. Do you have some better ways?
PS: If not, could you suggest me some links about how to implement these two methods?
Thanks a lot.
you can create a MasterActivity that extend Activity and contain your sidebar. other Activity can extend from your MasterActivity by this way you can access your sidebar on each activity.
I think you should use the default navigation drawer and default action bar.
If you are using the Android Studio then
right click on your package and go to New/Activity/Navigation Drawer Activity.
That will create navigation drawer fragment and activity automatically.
But If you are using Eclipse then these links will be useful for you.Navigation Drawer
Action Bar
you can use include tag in other layout activity and call Navigation Drawer:
<include
android:id="#+id/nDrawer"
layout="#layout/your_NavigationDrawerLayoutName"/>
So I'm working on adding ActionBarSherlock and the Navigation Drawer to a project that previously implemented a custom (very poorly written) "action bar". Instead of using fragments and a backstack of activities for navigation, some activities show and hide different layouts. (That is, suppose I am in a list mode and then select a button to go into an edit screen. The app currently hides the list layout and shows another layout.).
So I've added actionbar sherlock and a navigation drawer to all the activities. I want to be able to programmatically switch the navigation icon from the 3 lines to the arrow when certain buttons are pressed.
I can't figure out how to do this though. Any ideas?
Thanks!
The solution to this problem is to use the method:
setDrawerIndicatorEnabled(boolean enable)
inside the ActionBarDrawerToggle class.
After:
drawer.setDrawerListener(toggle);
Use this code:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.select);
It depends how wedded you are to built-in actionbar artifacts. You can always redraw the current actionbar by inflating a layout of your choosing, then calling
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayShowCustomEnabled(true);
// Inflate and do whatever you need to your view...
getSherlockActivity().getSupportActionBar().setCustomView(abView);
getSherlockActivity().getSupportActionBar().show();
When you want to go back to your standard (assuming you're using a DrawerLayout to do your navigation drawer), you can just set make a call to setDisplayShowCustomEnabled(false) (re-enable showHome and showTitle as you please).
As far as I know, customization of the back button can only be done via themes. Besides, swapping the drawer icon for the back icon (within the same Activity) doesn't make sense, since users would still be able to access the navigation drawer by sliding the left most edge to the right. It just wouldn't make sense.
If you absolutely need the back icon, then it would make the most sense to make that screen a new Activity since you would indeed be adding another "level" to the stack, which is what the back icon represents.
I am using HoloEverywhere + ActionBar Sherlock + HoloEverywhere's sliding menu addon. When clicking an item in the sliding menu, I start various activities.
My problem is to implement what Cyril Mottier talks about in his article about Prixing's SlidingMenu:
- overriding activity transitions (that's the easy part)
- saving/restoring the menu state between activities (that's what I can't manage to do): currently the menu disappears and it does not feel right. I would like to save the menu state and when the next activity starts, restore that state (and animate the close of the menu for instance)
Cyril talks about using the view's saveHierarchyState, ...:
The Activity transition in the Prixing application relies on the exact same technique the Android framework uses to restore an Activity after it has been destroyed in low memory conditions. As a result, everytime a new Activity needs to be opened, we save the the interesting part of the current UI state using View#onSaveInstanceState()/View#saveHierarchyState(SparseArray<Parcelable>) and re-apply it to the newly created Activity thanks to View#onRestoreInstanceState(Parcelable)/View#restoreHierarchyState(SparseArray<Parcelable>)
However I have trouble seeing how to implement that? Would anyone have any clue?
My activities all inherit from a base activity class which keeps its instance of the sliding menu.
See SlidingMenu issue
You can update code from the last version of SlidingMenu to same module of HoloEverywhere.
Or wait 1.4.3, where this module will be updated by me.