I have made a nice app with a whole bunch of activities , then i needed a navigation drawer and found out that you need to have only one activity for the whole app and the individual different screens should be fragments that are inserted at runtime.
my question is :
How to convert the entire app to use fragments instead of activities ? (eg: how to preserve activity hierarchy , show a main activity when the user opens the app , different actionbar for each screen , etc...)
There is no magic converter, you need to convert manually each activity to be extended from Fragment
and add few must methods like onCreateView for the fragment instead of SetContentView of activity.
Regarding the actionbars, it sits on the Main Activity so you need to create callback events from each fragment to the main activity in order to control the action bar.
Navigation drawer has nothing related to fragments.
If you wish you can put it into activities also.
What I created was a BaseActivity with layout having navigation drawer and all other activities extend BaseActivity so that each of your activity will have drawer. Only you need to change content page for particular activity.
happy coding.
Related
I have created an app with navigation drawer which I want to show on all the activities. And I want to show the same actionbar how can i do it?
I had created my app in android studio with
minSdk =15
and TargetSdk=23
And how can I fetch the email id of the user in navigation drawer .
The better way is , You have to implement fragment instead of seprate activities but if you have implemented activities and you don not want use fragment than you have to create a java class for navigation drawer and call the same class in each activity and also include the view in each activity but it is not good practice.
I have my MainActivity that contains a navigation drawer with items. When I click on an item,it opens up a new activity. This activity has its own layout file. I want to call the navigation drawer in this activity as well. How do I do that? Because I want to navigate through the app using the navigation drawer,rather than pressing back button all the time.
Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a DrawerLayout with the appropriate list view to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class: NavigationDrawerSetup.java. That way my activities can use the same custom adapter, etc.
Complete Reference : NavigationDrawer with Multiple Acivities
I have a fragment which I use as a menu bar on top of the screen to switch between activities and show which activity is currently open by highlighting its icon. It is hardcoded through XML in 4 of my activies. These activities are represented by icons and each icon has an onclicklistener which loads the appropriate activity when clicked. My question is, when I have a transition to switch between activities, is it possible to keep the menu bar in place and only switch the content of the activity like in Vine or YikYak with the sliding model. If this requires recoding all 4 activities I won't do it as it's only for eye-candy however I would like to know how difficult and time consuming this would be.
For the short answer, by replacing the Activity, you replace the entire UI, so no, it isn't possible.
The long answer is this:
Vine and YikYak don't replace the entire Activity, only the Fragments. So if you had a layout structured like this:
Activity
|-- Menu Bar
|-- Content
then YikYak and Vine only replace the Content Fragment, not the Activity. This is essentially what Google recommends with the DrawerFragment. In order to do this you would have to rewrite your entire app to make use of Fragment changes rather than Activity changes. I would recommend doing this despite the extra work, as its one of the main benefits to using Fragments at all.
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 trying to implement a tabbed navigation in my android app, however I want to run a different activity in each tab. I've been reading the android development page and they insist on using fragments for navigation over activities. From what I understand, you cannot have a fragment class by itself, it must be contained in an activity.
Is it possible to create a new activity for each tab and run that activity in the onTabSelected() function, while displaying the UI for each tab from the fragment within the running activity?
TabHost has been deprecated for Fragments, but I have been unable to find a way to navigate through activities by tabs.
Nope, you don't want to have separated Activities for each tab (although I've seen such apps :() . Basically - Activity is a top-container, and any TabHost View is a child of such Activity. Switch to the Fragments, check this out:
https://stackoverflow.com/a/6891923/1434631
this: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
and this: http://neilgoodman.net/2012/03/12/working-with-fragments-on-android-part-2/