In my main activity I have a ViewPager with two tabs and each of them has its contents in a fragment. One of the tabs holds a list of items and when I press on an item from that list I want the details of that item to appear on the screen. I thought of putting the whole ViewPager in a fragment and when the user taps the item the fragment representing the whole ViewPager is replaced with another fragment representing the details of the item. Is it possible and is it a good practice? Maybe I should just start another activity with the details of the item?
How you do it is your own choice. It's perfectly fine to switch your Fragments.
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FragDetails())
.addToBackStack(FragPager.class.getName()).commit();
Switching to another Activity is fine too, you just have more of a "flicking" screen when opening a new Activity (and some other life cycle stuff too of course). If you don't want that, switch Fragments.
Related
I'm using a bottom navigation view which has 2 menu items. Each menu item upon clicking loads a fragment onto the MainActivity.
The first fragment is the home fragment where a RecyclerView shows a list of items and the second fragment contains a simple form.
now whenever I switch back and forth between these fragments, their previous states are lost and the fragment is recreated. This is a big issue for me because suppose I scroll to, let's say the 15th item of the list present in the home fragment. After scrolling, I navigate to the second fragment and then I navigate back. What happens is that the home fragment gets recreated and to see the 15th item I have to scroll again.
What should I do to handle this?
Currently, my app is using a navigation drawer for my fragments. However, I have 2 fragments that are related, I would like to put these under one heading on the navigation drawer and then have 2 tabs to switch between the related fragments once I select that choice on the drawer.
Is this possible with only one activity or would I have to implement another activity specifically for the tab? I've considered only adding the tabs once the right fragment is committed but that leaves the question of how to remove it once I switch to a fragment that doesn't require the tabs.
I'd also like to avoid having to implement another activity as I am trying to keep the navigation available through my whole app. I'd rather not have to create an identical drawer and then have to keep things consistent between the 2 drawers.
Edit:
So far, I've managed to make a parent fragment that has a viewpager, fragmentpageradapter, and tablayout. Swiping between the 2 tabs does indeed shift between my 2 desired child fragments. However, I am unable to change a textview in one of my child fragments from my MainActivity. I'm guessing the reason why this is the case is because technically, my parent fragment is in view, not my child fragment. Any suggestions to get around this?
Hierarchy:
MainActivity
-Navigation Drawer
--Fragment1
--Fragment2
--Fragment3
--Fragment4
---FragmentPagerAdapter (under Fragment4, tabs)
----SubFrag1
----SubFrag2
Here's how you might use FragmentStatePagerAdapter to swipe across a Fragment:
http://developer.android.com/intl/es/training/implementing-navigation/lateral.html
I hope it help you.
I made a tabbed activity with two fragments and i want the activity to be locked at fragment1 unless the user click a button in fragment1 then the activity switch to fragment2 automatically and the swipe is unlocked so you can switch between fragment1 and fragment2. I have been learning java for a week now and creating this tow fragment takes me a lot of effort and headache and since i have not studied java or other programming languages i think i can't figure this one on my own please help me.
That's very simple. If you want to work it with a button, add button inside the tab, and at Onclick View call the 2nd fragment method.
I have a Fragment activity which hosts a fragment list. The fragment activity also has a couple of text views which summarise the data in the fragment list.
FragmentActivity = Layout that contains a fragment container, text views and a button.
ListFragment = goes inside of the FragmentActivity's fragment container and is a list of data
This works fine as an activity, but now I'm trying to put the whole lot into another fragment container to display it in an action bar tab on another activity. I can only get it to work with the ListFragment (obviously this is because the FragmentActivity can't go inside of a fragment container) but this means that the summary text views and the buttons are missing and I really need them there too.
I wondered if anyone had suggestions of the best way to implement this? Some thoughts of solutions I've had:
Change the Layout of the activity holding the tabs if this certain tab is clicked
Programmatically add the text views and button to the fragment container if this tab is clicked
Add another fragment to the fragment activity, below the list fragment
Either way I'm stumped so any help is really appreciated.
Create a Fragment that resembles the layout of you FragmentActivity and add it instead. you can't add the FragmentActivity as a Fragment simply because it's not a Fragment but an Activity that was added a Fragments support for older versions.
So instead of adding you button and TextViews directly to the FragmentActivity Layout. Create a Fragment with the same components and add it to the Activity layout.
That way could could reuse this set of components in another location in your application if you needed to, using the same Fragment.
I have one fragment that deals with previewing and so taking pictures, and on that fragment I want to have an option in the action bar have an item.
On some of the other fragments I would like to have items that will be in common, but only if not on a large device (tablet), as the tablet will have a fragment that handles controlling activities.
So, since I only have one Activity in my application, what is the best way to have context sensitive menus in the actionbar, in a fragment.
And how do I best show items only if on a small device?
It appears the best approach is to just have the fragment add the items to the actionbar as described here:
http://developer.android.com/guide/components/fragments.html#ActionBar
But, it will also be necessary to look for an id that is unique to the non-large layout to decide if certain menu items should be included.