I have some questions about designing applications with NavigationDrawer.
I create an application with NavigationDrawer composed of one Activity and some Fragments. For example I have four Fragments, and every Fragment has some actions.
Which is the correct way to implement that?
Adding all actions to my Activity?
Create some other classes and call them from MainActivity with context/view?
Other?
Any information about these questions is really appreciated. I also need some example or resource explaining which is the correct way to implement this.
Anyone can help?
Check the google IO 2014 source code to get a good idea of the nav drawer implementation
https://github.com/google/iosched
All the navigation and fragment transactions should be done via the Main Activity or the activity holding the fragments. Any kind of fragment - fragment communication should be avoided. The developer docs shows a good example of how to handle fragment communication via Activity using interfaces
http://developer.android.com/training/basics/fragments/communicating.html
Related
Some background:
Coming from an iOS background, using UITabbarController is very common and straight forward. Each Tab in the tab controller will change the current view to another UIViewController, and each of these UIViewControllers can have its own NavigationController (which kind of acts as a back stack). So whenever I switch tab, I would resume to the state where I left off.
Now I want to implement the same thing in Android, but it seems like the use of ViewController is different in Android. After digging around, I read that instead of using Activity like UIViewController, I should use Activity to act more like NavigationController, and use Fragments (which is deprecated)
to act as UIViewController instead.
However my question is:
Should I be implementing multiple Activities for Bottom Navigation? When I click on each item in the Bottom Navigation should I use an Intent to change Activity? Because from my understanding, using Intent to change Activity will add the new Activity to an Activity back stack, which would prevent me from switching back to whichever Activity I want. If someone could, Please tell me what is the "right" way (if there is one) to structure Bottom Navigation. Thank you all in advance.
You can use fragments as UIs, And Use a BottomNavigationView in your activity or you can use some libraries.
Here is a library for better customization: https://github.com/ittianyu/BottomNavigationViewEx
Native Method:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
In Android you should use Viewpager, tablayout and Fragments. Just search for its tutorials. there are lots of them on internet
Hello i am fairly new to android development and have been using Activities to navigate through my app. Now that i have gotten my feet wet i'd like to write more efficient applications. From what i have read, fragments are a way to limit the creation of unnecessary Activities and reuse UI elements.
I am trying reduce the amount of activities in my applications by converting them to fragments. When a button is clicked, do i call the next fragment from the current fragment, is this good or bad practice? What is the role of the main activity if fragments are directing the flow of the application?
Fragment is good practice but you need more careful handling fragment because it's tight coupling is when a group of classes are highly dependent on one another.
read this tutorial it will help to understand fragment flow [https://guides.codepath.com/android/creating-and-using-fragments]
Fragment is view and Activity is parent View (MainController is must be actvity)
it is me again and i first wanted to apologize for asking that much today.
Here is my situation.
Because of my design my FragmentActivity( topmost parent in obiect hierarchy) has only one member, which for itself has a collection of objects like fragments, i have one level between fragmentactivity and the fragments. I would like to avoid some kind of event bus and/or observable pattern to pass the events of the fragments buttons to my object which would forward them to fragment activity to be then handled there.
Especially showing fragment, deleting replacing fragments is preferred to be handled in my object in between or even in the fragment itself.
Is this recommended? Or will I get into any trouble?
So I have a TabActivity that branches into three sub-activities (tabs). One of these activities is a ListView, which I want to branch into further ListView activities. However, I want each of these branched ListViews to also have the same tabs at the top. To do this, do I need to create a separate TabActivity and a separate Activity for each of these branched ListViews? Or is there an easier way?
Cannot you trick a user into having TabActivities, but instead simply have one ListView instance with 3 buttons on the top of an activity (Tabs) and every time user clicks one of the "fake" tabs, just refresh the existing ListView with views that are appropriate for one of those "fake" tabs. In my opinion this solution would be more efficient resource-wise and render time -wise (which anyway are dual concepts)
This is what I have in mind
You may use Fragments as proposed by MaciejGórski for TabActivityOne, TabActivityTwo and TabActivityThree, while the ListView inflation technique could still be used
Switch from the old deprecated APIs using like TabActivity or ActivityGroup into Fragments.
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
From TabActivity documentation.
I read quite some articles about fragments, but I am still confused about how to do what.
I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button
android:onClick="buttonClicked"
Now I want to implement that method
public void buttonClicked(View view)
I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...
I'm not sure what the specific problem is, but maybe this will help.
From the Android documentation on Fragments:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).
On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).
Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).
Well,
I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.
Check it on.
Android developer onClick attribute description
I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.