I have recently decided to add a Navigation Drawer to every screen in my app. I already have the working code. Is there a faster way to add the same layout to every screen or must I do this manually?
One way you could do it is to create an Activity or Fragment class say BaseActivity that all your Activity or Fragment will inherit, and put your Navigation Drawer logic there.
Related
I have a project containing different activities like: main activity, signup activity, login activity and track activity. Now I want to create another activity which will contain navigation drawer. And the point is "I don't want to use navigation drawer at any other activity".
All the example so far I found at searching # net provide example using main activity. But I don't want to use navigation bar at main activity.
If you are using Android Studio, follow these steps,
1. Right click on app.
2. new -> activity -> navigationDrawerActivity
this way only your new activity will have navigation drawer.
Currently, my app is using a navigation drawer for my fragments. However, I have 2 fragments that are related, I would like to put these under one heading on the navigation drawer and then have 2 tabs to switch between the related fragments once I select that choice on the drawer.
Is this possible with only one activity or would I have to implement another activity specifically for the tab? I've considered only adding the tabs once the right fragment is committed but that leaves the question of how to remove it once I switch to a fragment that doesn't require the tabs.
I'd also like to avoid having to implement another activity as I am trying to keep the navigation available through my whole app. I'd rather not have to create an identical drawer and then have to keep things consistent between the 2 drawers.
Edit:
So far, I've managed to make a parent fragment that has a viewpager, fragmentpageradapter, and tablayout. Swiping between the 2 tabs does indeed shift between my 2 desired child fragments. However, I am unable to change a textview in one of my child fragments from my MainActivity. I'm guessing the reason why this is the case is because technically, my parent fragment is in view, not my child fragment. Any suggestions to get around this?
Hierarchy:
MainActivity
-Navigation Drawer
--Fragment1
--Fragment2
--Fragment3
--Fragment4
---FragmentPagerAdapter (under Fragment4, tabs)
----SubFrag1
----SubFrag2
Here's how you might use FragmentStatePagerAdapter to swipe across a Fragment:
http://developer.android.com/intl/es/training/implementing-navigation/lateral.html
I hope it help you.
I followed this tutorial about navigation drawer androidhive-navigation drawer and it has used fragments for the list items.
Similarly all sources I found use fragment views for individual list items. I have these issues.
Why use fragments, when we can use an ordinary Activity class?
Going with the link example, if I have Home Activity class implemented before, what should I do to convert it to a fragment?
If Home is the main activity, what happens then?
I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM
This is a relatively general question that I have regarding to Android development.
In the Android application, I am using SlidingMenu library. Imagine the activity I am trying to implement has a Navigation Drawer (from Sliding Menu Library) and action bar tabs with View Pager and contains different fragments.
In order to have the navigation drawer in the activity, I had to inherit the application from SlidingActivity like this:
public class ActivityMain extends SlidingActivity implements TabListener {
However, to make View Pager work in this activity, I will need to use Make a FragmentPagerAdapter instance, and if I want to use it, it requires the activity extend the Fragment Activity.
My Activity already extended SlidingActivity, so there is no way to extend another super class. I am not sure what will be a proper way to solve this conflict. When I was working on the Android app, I have saw some other cases that different components in one activity requires to extend from different super class. What will be the general solution to such problem?
Thank you
If you look at the package of SlidingMenu here, they have an activity called SlidingFragmentActivity. Extend this activity instead of SlidingActivity gives you everything you need.
You can wrap your Activities in a SlidingMenu by constructing it programmatically new SlidingMenu(Context context) and then calling SlidingMenu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT). SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.
from https://github.com/jfeinstein10/SlidingMenu