How set action bar options menu when a nested fragment becomes visible? - java

I need to be able to clear the action bar menu and inflate a new one from within a nested fragment when the particular nested fragment becomes visible.
My app consists of a main activity with a ViewPager which uses a FragmentPagerAdapter. This ViewPager has 4 root fragments which are simply blank FrameLayouts (one in each page) that each contain and swap out nested fragments. These nested fragments can be different or the same type (think about how Instagram can show a different profile view in multiple tabs). This means that I can't use the onPrepareOptionsMenu to inflate menus on page swipe, because I won't know which nested fragment is in the root fragment of each page. Additionally, I have set the offScreenPageLimit to 3, so that I can swipe between root fragments without reloading. This means that I can't just inflate the menu from within the nested fragments, because then all four nested nested fragment's menus will appear.
The ideal solution would be able to inflate menus from within the nested fragments through some sort of callback that gets executed when the nested fragment is literally displayed on the screen, e.g. when we swipe to the page containing the root fragment which contains the nested fragment, or we swap the nested fragment for another within the root fragment of a page. Does anyone have a design solution to the problem I'm having? Thanks for any help!

The ideal solution would be able to inflate menus from within the nested fragments through some sort of callback that gets executed when the nested fragment is literally displayed on the screen.
Create a menu for the fragment in the menu directory
Report that this fragment has options menu have a look
Then just inflate it here, and override this method and when the fragment will become visible those menu options from fragment menu will be added to activity menu options.

Related

combine tab view with bottom navigation

In my app I want to combine a tabView with a bottom navigation bar. I want to have three tabs and three options in the bottom bar, so there should be nine different activities in the end. My tabs and the bar are defined in my activity_main.xml, so for every selected option in the bottom bar the tabs remain the same. How can I open a new fragment which depends on the combination of the selected tab and selected bottom bar option?
I suffered from a same problem before and here is my solution.
You will need 1 activity and 12 fragment not 9 to achieve it.
The main activity contains the bottom navigation bar, and inflate 3 fragment let's name them LeftFragment, MiddleFragment and RightFragment, this main activity should act only as an inflator for this fragments, don't write any other code in the activity just a manager for the bottom navigation bar.
Each one of this fragments should act as a holder or a manger for the tabs fragments, agian don't write any code in LeftFragment, MiddleFragment and RightFragment just a manager for the tablayout.
And your actual layout code should be in each tab fragment.
Hope this answer your question.

show another xml by clicking a button without creating another activity

Is there any way to avoid creating another activity to show an xml page from another xml by clicking a button.
The same activity must be used instead of creating another one in this case.
You can inflate your XML View to one of your Layout in Activity's xml Layout file.
1. Create Blank RelativeLayout and set visibility invisible.
2. On your action performed set Visibility true and inflate view to Relative layout like below code.
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Welcome to the Android world , check this out
Fragment

making a View like Fragment Drawer

I would like to make a Fragment show up just like a Fragment Drawer would, but not only to a list of intents.
(If you don't know what I mean by Fragment Drawer):
Google products such as Gmail, "Google app", Google Play... utilize them frequently.
The Fragment Drawer is a menu on one of these apps created by swiping from left to right or clicking the "Hamburger" button.
I would like to highlight the distinction between what I want and the Fragment Drawer is: I would like to be able to place TextViews, ImageViews, and Buttons inside of it as if it were a normal fragment in which I could just inflate a layout in it and I would rather it come from the right instead.
If a visual representation is needed I would be happy to try my hand at making one.

Tabs on top and bottom of the screen

I'm trying to build a view that will have standard tabs on the bottom and action bar tabs in the upper part of the screen.
I've tried using FragmentTabHost together with TabLayout but with no success since they both call getSupportFragmentManager() so one cancels another.
I've also thought about using deprecated TabLayoutActivity but I'm using AppCompat theme so all my activities have to inherit AppCompatActivity.
Is there a way for bottom tab to start activities and upper one fragments, or at least both starting fragments that work?
You can have 2 framelayouts (bottom,middle).
On bottom framelayout you will have a fragment with buttons as tabs, when you click on each button you will send back to activity which button clicked.
On middle framelayout you can have a TabLayout fragment and if you prefer also 1 fragment for each tab in the tablayout.
One of the solutions is to use getChildFragmentManager() available in the support library to have a Fragment inside another Fragment.
The outer one with FragmentTabHost can be controlled by main fragment manager getFragmentManager() and inner one with TabLayout by child fragment manger getChildFragmentManager().
Here's an example with nested fragments.
Still looking for a viable solution with Activity and a Fragment.

Inflate layout on page change in viewpager

In my app, I have a view pager with two tabs and I have two layouts and I want to inflate and show layout1.xml inside the tab when the user switches to tab #1 and inflate and show layout2.xml inside the tab when user switches to tab #2, but I cannot find how to do it.
Any help will be very much appriciated.
Create two fragments
Inflate one of the layouts in each fragments onCreateView() and return the view.
Return an instance of fragment 1 for the position 0 in your FragmentPagerAdapters getItem() method and an instance of fragment 2 for position 1.

Categories