Multiple fragments or nested fragments - android

I am writing an android app that will have a number of different screens that I would like to swipe between, each screen will be a full page except for action bar header. On each screen there is the ability to open up another screen which will also be multiple screens that I would like swipeable. What is the best way to handle this. Do I have one fragment manager that holds all the screens and handle the onPageScrollStateChanged to only allow swipes between the current accessable screens or would I be best off nesting the fragments. I hope the above makes sense.
Thanks in advance

Sounds like you want to use a ViewPager to to swipe between views (Fragment extends View)
You could either:
In a single activity use a FragmentManager that switches between the parent and child Fragments, each with their own ViewPager and nested Fragments
Start a new activity to hold each ViewPager
Both are valid, if the Fragments need to communicate with each other or the Activity option one might suit the project needs better.

For the swiping between views you indeed need a ViewPager
For the nested fragments I would use a wrapper. I struggled a lot with fragments and found that this is the best way. A wrapper is very simple. This is just a fragment that holds other fragments. In the onCreate() of this fragment you get the childFragmentManager and add the fragment you originionally wanted to add. If you want to go to a new fragment you simply get the childFragmentManager again and replace the current item. This way you have nested fragment. You can add this to the backstack in order to get back navigation, but you need to override onBackPressed() inside your activity and call the method popBackStack() from the fragmentManager in order to get the first fragment back.
If you have any questions, comment below.

Related

How to use an activity component in different fragments with viewpager

I have an activity with 5 fragments. i'm using different functionalities on the same component's onClick inside different fragments. because of viewpager's fragment preloading, functionality of next fragment is executed instead of current fragment. what should i do?
There is no way to avoid it because of view pager has minimim offset is 1.
So I would suggest to you using another way to archive your result:
Idea 1: If you are using FragmentStatePagerAdapter second parameter as FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, it means only one fragment inside your view pager will be in onResume state. Therefore you could start all of your networking/side effects job in onResume method.
Idea 2: Why do you using view pager, when you do not need swipe left/right effect? Probably you are in the wrong way. You could use for example just frame layout, then with your fragment manager attach and detach fragment when a user interacts how you expected. I would advise you to look better at FragmentTransaction. Here is a link.
Feel free to ask me about one of these approaches.

Transitioning from Fragment A to Fragment B using two recyclerviews?

Right now I've Fragments A with Recyclerview - where I've categories (Image+text).
I want to make Fragment B with Recyclerview - where I've types (Image+text). Same layout, same everything except text/image.
Like this:
https://img.exs.lv/e/z/ezeliitis/frags.png
For instance, I click on Fragments A - first picture (Cars) and it opens Fragments B - in same layout as fragment A, which contains (AUDI, BMW, OPEL ect...). Should I just make copies of fragment A (adapters/viewholders ect.) changing db names/pictures or is there some way to "DRY" the code? Also, isn't it bad having two recyclerviews (performance) ?
Also, movement from one fragment to another is called fragments "..."(what exactly?)
Same layout, same everything except text/image
You must need to replace the RecyclerView adapter, then. No need to start another Fragment, but you're more than welcome to.
isn't it bad having two recyclerviews (performance) ?
Not that I know of. You'd only have one at a time, from what I understand anyways.
movement from one fragment to another is called fragments "..."(what exactly?)
If you do need to switch Fragments, then you want the FragmentTransaction class of the host Activity. That's how you switch. Documentation is pretty good with its example.
https://developer.android.com/training/basics/fragments/communicating.html

Show nested fragment on screen rotation without showing parent fragment

When rotating the screen my nested fragment is shown but for some brief moments, the parent fragment is also shown.
I have my MainActivity that has a FrameLayout with ID activity_base_container.
I'm doing this when my activity starts:
Fragment initialFragment = getInitialFragment();
mFragmentManager.beginTransaction()
.add(R.id.activity_base_container, initialFragment, initialFragment.getClass().getSimpleName())
.commit();
That initialFragment initial fragment is responsible to check some conditions and depending them will launch one of two possible fragments:
fragmentManager.beginTransaction().replace(R.id.activity_base_container, fragment, fragment.getClass().getSimpleName()).commit();
Lets assume it launches FragmentF (whit a root FrameLayout with id fragment_f_root). This fragments layout has a set of options. When the user clicks one of those options, the corresponding fragment is created and is launched like this:
//The example here is an option that displays a google map.
fragment = FragmentMapMultipleActivity.newInstance();
fragmentManager.beginTransaction()
.replace(R.id.fragment_f_root, fragment)
.addToBackStack(fragment.getClass().getSimpleName())
.commit();
At this point all is working as expected. The problem is when I rotate the screen. FragmentF appears briefly and then immediately FragmentMapMultipleActivity, the nested fragment, appears.
Is it possible after rotating the screen show only the nested fragment or I should change my "architecture" to something else?
should change my "architecture" to something else?
Probably, you should.
The brightest Android-minds from Square are even advocating to avoid simple fragments everywhere it's possible: Advocating Against Android Fragments
Nested fragemnts, in its turn, increase complexity exponentially. The only good pattern of using them I've seen so far is ViewPager with it's FragmentPagerAdapter. In majority of other cases, consider using Custom Views instead.
It keeps your app's lifecycle cleaner and more predictable.
I don't think you can do much with this blinking you see, apart from:
setRetainInstance(true) and avoid full re-creation of the Fragment in Activity, so you keep you fragment's data during change of the configuration (and then pass same retained fragment to the fragment manager)
keeping layouts as lightweight as possible
avoid re-creation of already initialized variables
keep onViewCreate() as lightweight as possible
Good luck!

Android - how to slide screens, inside a fragment

I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.

fragment inside a fragment in android

In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.

Categories

Resources