I want to use multiple fragments in each tab of Tab Host.
I am googling for last 5 days but nothing is working in my case. I got a good working solution
Seperate Back Stack and Sample Project Here .This maintain a separate Custom Back Stack for each tab having lots of fragments and store Fragment object in Custom Stack. But when ever I want to re-add any fragment that I already created and stored in its custom stack(as an object), all it's life cycle methods are called once again as are called first time. This is the problem. In this case all the views of fragment layout are recreated and it behaves like a new fragment.
I want to implement functionality like tabs having activity group(in which lots of activities are combined in a single tab using activity group) with the help of fragments.
Please help me in solving this issue........
Related
I would like to create a good base for my app and have it working efficiently. I am a bit lost still with understanding the relationships between fragments and activities (in the basic form I get it, but in cases like the one I need to deal with now I am not sure what to do).
Currently I have 3 fragments in a View Pager, very simple stuff. one of them is for gallery, one of them is for profile and one of them is for forum (imagine the question board here on SO).
Now, inside the forum fragment I have a FAB that should open a new page for asking a new question.
What I am wondering about is, should the new question page be a fragment or an activity? Also, when a question is clicked I would like its thread to open with all the comments. That should be a fragment as well correct?
If fragment, where do I load it into? The View Pager? If so how? Can the view pager host a fragment temporarily? Because the fragment should only be visible when the button has been clicked. Should I place the fragments inside of the forum fragment so it'll serve as it's parent fragment?
I can make it as an activity, but by what I read and know about fragments, it makes sense that it should be a fragment because it is part of the same activity, but when that is that case I am not sure how to do it.
I would actually not use a ViewPager in your case. I use ViewPagers for a series of related screens that deal with a very specific task. For example - a wizard that details instructions where each Fragment might represent a step and it's useful for the user to be able to slide back and forth between instructions.
Given your requirements I would still host everything under one Activity and the individual pages as Fragments inside that activity. Then maybe use something like a bottom nav bar to navigate between the profile, gallery and forum. The screen that is launched by the FAB and another one for viewing the thread of a forum post should be also be separate Fragments as well.
This might be a good opportunity for you to try out the navigation framework under Jetpack. It makes these kinds of things a lot easier to visualize.
See here Android Navigation Framework
I am new to Android programming. I started building an app using YouTube tutorials but I am a little bit confused and facing a problem. Should I use fragments or activity in my sliding tab? I am working on an Android project of employee attendance and payroll so I am thinking of using a slide tab (something like the screenshot below). The 1st tab may contain a form to add an employee and after adding it, will display information in list view and the other may contain salary. Should I try fragment or activity for my tabs?
A fragment is always located in an activity.
So you wil always need an Activity and then add the fragment to the activity.
if you say you want to slide?
you probably mean a viewpager, that can have n fragments. the viewpager needs a special viewpageradapter. where you define how many fragments you want. and at which index what fragment needs to be shown
I used to have 3 activities with 3 layouts.
1. HomeActivity.java (activity_home.xml)
2. HelpActivity.java (activity_help.xml)
3. SettingsActivity.java (activity_settings.xml)
Whenever i had to open other activities, i used animated slide-in-out intents using overridePendingIntent. In this way, all the variables of HomeActivity stayed in itself and HelpActivity's variables/methods were in itself and same for SettingsActivity. I used onCreate() in these to perform some activity specific code. and, android:onClick="fetchSarcasm" from activity_home.xml was calling specified method in HomeActivity.java. So, there were no conflicts. All the normal things, that happen in an AndroidProject with 3 activities and used intents to switch to one another, were happening as i wanted.
But now, I wanted to migrate to ViewPager instead of slide-in-out intent transitions. In this way, there will be only one Activity with an XML of ViewPager. This ViewPager and its FragmentPagerAdapter gets the Pages from 3 classes extending Fragment with relative XML layouts.
I want to ask: Where do i write my Page Specific Code for onCreate(), layoutView's onClick="" and for onChangeListeners. Because, 3 classes extending Fragment do not have any Context or findViewById() and they do not respond to android:onClick="method" in different layouts.
Do i need to mix all that code of three activities into one containing ViewPager? If so, this makes ViewPager activity pretty heavy. and, writing if(page=2){ dothis(); } seems absurd for all pages at every code point. Any checkbox(view) that is in page 3, becomes null in other pages.
I know this might be a stupid question. I have spent almost 4 days trying to achieve something but i can't. Also, this is just my 2nd month in android, so i am new. I do not use Action Bars, Tabbed Bars or Navigation Drawers.
I am available on SO Chat too if you want to ask something more.
I just want to know how do i merge all the work i did when I was not using ViewPager.
If you're only reading the Preferences once for each Fragment, you can move your previous Activity.onCreate() logic to each Fragments onViewCreated() or onCreateView(). Read your shared Preferences there and set your Checkboxes accordingly.
Fragments inside a ViewPager aren't necessarily recreated on each page change. You can change how many Fragments are instantiated at a time with ViewPager.setOffscreenPageLimit(int).
Fragment Lifecycles explained
first, I'd like to state that I'm new to android development and to gradle as well. not to java.
I had a project supporting 2.3.6 and I'm now in the process of migrating it to 4.4 using the support library.
I'm using Gradle as well, and so far, it's working great.
I'm now using the navigation drawer to navigate between the app's fragments, which used to be activities.
One of the fragments in the drawer holds a listview and a detail view for an item in the list.
The drawer fragment switches it's view when the user clicks an item in the list and shows it's details.
While it's pretty clear to me why it's not recommended for 2 fragments to communicate directly, I still need the list fragment to tell it's details fragment counterpart about the selected item.
The guides specifically say that the activity should implement the callbacks and on the callback methods it should communicate the data between the fragments.
However, in my case, the parent is also a fragment.
My question is should the parent fragment maintain the callbacks?
It seems like the proper solution, since my fragment is only implemented this way to use the navigation drawer swap, and logically, it behaves like an activity, but this would go against the guides saying fragments should not communicate directly.
The question is if implementing the callbacks in the containing fragment is a viable approach or i should change this part of the app to an activity?
Thanks, and sorry for the wall of text.
Your parent fragment should be swapped in via the NavigationDrawerFragment whose callbacks should be implemented in the Activity. The callbacks shouldn't have anything to do with the data passing if I'm understanding you correctly. If the detail is a child of the parent fragment I don't see any reason that the data can't be passed when the child is created.
Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.