Managing multiple fragments next to each other - java

I am currently working on a tablet app which has 1 main activity. In this activity there are 6 buttons which represent the main menu. When clicked a fragment opens, which contains a submenu. When a submenu item is clicked another fragment is opened.
At this point I have 2 active fragments in 1 activity.
My problem is that when I click a new 'main menu' button just 1 of the 2 activities closes, the first one that was opened. I think it works like a list, first in / first out when the third activity is opened.
When I use replace and only one fragment is active there is no problem, it is closed and the other one opens.
How do I manage more than 2 fragments?
Kind regards.

I have found the awnser myself, I have to give every Fragment a name and add them to the backstack.
ft.addToBackStack("fragmentname");
When a 'main menu item' is clicked I remove all fragments from the backstack:
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
When I wish to selectively remove fragments from the backstack when I open een sub menu I use:
getFragmentManager().popBackStack ("film", FragmentManager.POP_BACK_STACK_INCLUSIVE);
I hope this helps some people facing the same problem.
Kind regards

Related

How do I save the state of fragments in android?

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?

Android - add Swipe View/tabs to only SOME fragments

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.

tabbed activity fragments swipe unlock if button is clicked

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.

Putting ViewPager and tabs in a fragment and replacing it

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.

Should this be 2 fragments or 2 activities?

I have a tabbed interface for my program - there are two tabs: take photo and view photos.
As the name suggests, the user can take a photo in "take photo" and the user can view photos taken in "view photos". Right now the way its set up I use one single MainActivity and I have TakePhotoFragment and ViewPhotoFragment -- question is: does this contradict the principles in which Fragments are supposed to be used in? I don't really anticipate having both fragments displayed in a single screen (e.g. on a tablet), but I don't see how I can use one activity for each because of the limitations of the tabbed interface (when I created the activity in eclipse, I was prompted to select what kind of layout, I chose tabbed layout, and automatically code for fragments within an activity corresponding to several tabs was generated)
Can anyone help? Should "take photo" and "view photos" be fragments or activities?
It should definitely be fragments.
This does in no way contradict anything, plus I do not understand your concern about showing both fragments in a single screen. If you do not want that to happen, you just program accordingly. That is certainly not something that just happens because of the choices that you have mentioned so far.
Fragments is the best method you can use for the purpose you mentioned above. You can check the below links to know about the usage of fragments.
Creating a fragment
Fragments
android fragments
android fragments tutorial

Categories

Resources