I have an application with an FragmentActivity called MainActivity which holds two fragments, one menu fragment to the left and one content fragment to the right. On a large device both of these fragments are shown. On a small device only the menu fragment is shown and when the user presses an item in said menu the content fragment is displayed in a new Activity.
Lets say the user is currently using the application on his phone and only the menu is visible:
The ActionBar of MainActivity can be manipulated by user choices in the menu. When the user selects a specific menu item, a new Activity launches to display the content fragment. When this Activity pops up the ActionBar of this new Activity is a new ActionBar and therefore does not resemble the ActionBar of FragmentActivity (MainActivity).
I figured I could pass the ActionBar from MainActivity to the Activity which displays the content fragment through Intent extras, or making the ActionBar static and reference it like MainActivity.actionBar or similar, but I ran into problems very quickly :)
Any suggestions on how to solve this? Basically, what I want is to share a single ActionBar reference between all my activities.
Maybe it's better to use fragments instead of activities. I mean show menu fragment, and when you need to show content fragment just put content fragment into the place (container) where was menu fragment. I think better/easier to organize fragment replace/change logics than trying to share actionbar between different activities.
Related
So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.
I want to implement a Navigation Drawer in my app but I am conflicted on whether I should use it with Fragments or with Activities (see image below for more details).
Is there any real advantages or disadvantages between the two or is it just a matter of preference?
Edit:
Just to clarify my question:
In the case of using Activities instead of Fragments;
When I select "Import" that will open an Activity and not a Fragment and if I select "Gallery" it will open an Activity with contents for gallery item etc. and so on for the other items in the Drawer window.
In the case of using Fragments instead of Activities;
If I choose from any of the Items in the Drawer window it will open their contents in Fragments for each Item selected instead of starting new Activities for each selction.
Remember Fragments need an Activity. You always have one minimum when using Fragments.
If you are talking about to use like main element in the most cases is best use fragments because you have more flexibility UI.
The performance would be better if you have 3 activities and 10 Fragments or have 13 Activities? Think about it, the navigation within the App would be the big challenge but it's just about using the right flow in your application.
Edit:
For instance:
Drawer With Activities instead of Fragments
If you were to use NavigationDrawer without Fragments then it would be best to keep the NavigationDrawer instance in a single Activity and when you navigate the app by choosing from the items in the NavigationDrawer then each of those Activities that are started should not implement the NavigationDrawer but instead should implement the back button to Navigate back to the "Main"/single Activity that the NavigationDrawer was implemented in.
Note: If you do want to implement the NavigationDrawer in multiple Activities you would have to recreate a new instance of the NavigationDrawer in every Activity that you desire to display it.
I suppose this would be a disadvantage vs using Fragments whereas if you used a fragment you wouldn't need many instances of the drawer you would only need one.
Drawer With Fragments instead of Activities
If you use the NavigationDrawer with Fragments then the drawer should be implemented in a single Activity and when each drawer item is selected, their contents are displayed in each of their very own Fragments(which is called inside of the central Activity which manages all the Fragment instances)
First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.
I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.
I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
Im doing an app with navigation drawer.
For this, i have a HomeActivity, this contains all the login of my navigation drawer, the options in menu, the view, the titles etc. And here, i set listenerclick for navigation elements.
This listener receives FragmentManager and with a switch do:
smf.beginTransaction().add(R.id.frame_content, new Fragment()).commit();
Replacing fragment for the fragment that i need in each case of switch.
In home layout i have a framelayout and navigation drawer.
Mi question is, is correct that i only have one activity with a framelayout, and depends on the item clicked in navigation drawer i replace the fragment on the frame, or is better have lot of activitys, and create menu in all of them with the same login, and when user click in item menu, launch new intent with the activity selected?
I hope i have explained ok...
Thank you.
I did this same thing, but I found it was a lot better to have different activities.
If you do go down the separate activities path you should have one base activity that the activities extend so you don't need to rewrite the drawer code.
A fragment is only really meant to be an extension of an activity, for example when you have multiple tabs, or you are swiping between different views, or you need to break up your activity into sections.