Fragment visible on the background - android

I am working on an application that has three fragments which are defined in an XML file:
[HeaderFragment]
[MainFragment]
[FooterFragment]
The first screen initiates the three fragments, the Header- and FooterFragment are static so will not change content.
The MainFragment is initial a menu with buttons and a transparant background (MenuFragment). When I click an item in the menu I replace the MenuFragment with a new fragment (DetailsFragment) like this:
FragmentTransaction transaction = mFragmentManager.beginTransaction();
Fragment newFragment = new DetailFragment();
transaction.replace(R.id.content_container, newFragment);
transaction.addToBackStack(newFragment.getTag());
transaction.commit();
The DetailFragment shows up and when I press back, the MenuFragment appears and everything works how it should.
Here is my problem:
Inside my DetailFragment, I have a toggle option to filter the content, this is a button. When this is clicked, the DetailFragmentFiltered replaces the DetailFragment on the same way as the code above states. The only difference is that I don't add it to the BackStack because after filtering and pressing Back.. I still want to return to the MenuFragment.
When I clicked the filter button and press Back, the DetailFragment (or DetailFragmentFiltered) is shown behind my MenuFragment. Afcourse, I don't want this.

Make sure you don't use a static fragment relation to an XML by setting the first fragment as "android:name" in the layout.
Make with framelayouts the layout of the XML and add the fragments flexibly as shown in this tutorial:
http://developer.android.com/training/basics/fragments/fragment-ui.html

Related

Working with fragment above Main Activity

Im working with fragments on my main activity I have a Edit Text and buttons in my main activity But when I opens fragment page and click exact at the spot of Edit Text the keyboard activates.
So it means the below activity is also in working at same time
Fragments are designed to be inside an activity, if you don't want to make your activity visible you can use a full screen layout for fragment, to remove the clicks you can replace .add command with .replace when transacting to fragments give a background color to the root view of fragment layout also. I think this would help
YourFragment fragment = new YourFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.ll_job_view_fragment_container, fragment)
.addToBackStack(TAG_FRAGMENT_YOUR_VIEW)
.commit();
Edit
The replace may not help, If you inflate different fragments in the same container you just give the attribute android:clickable="true" to the top most parent view of each fragment that will help.

Android Fragment - add and bring to front

The thing I'm trying to achieve should be quite simple but I can't manage to find the solution to my problem.
I have fragment A. When fragment A's button is clicked, fragment B is displayed:
#Override
public void onClick(View v) {
Fragment fragment = new PostMeasurementFragment();
FragmentTransaction fragmentTransaction = ((PostActivity)context).getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.add(R.id.post_container, fragment, "PostMeasurementFragment");
fragmentTransaction.commit();
}
Fragment B has a transparent background so I'd like fragment A to be visible behind fragment B. If I use .replace rather than .add, I can see only fragment B and that's make sense because fragment A was removed by the .replace method. When I use .add I can't see fragment B though and I know it's there because of .addToBackStack: I need to click the back button twice to get rid of fragment A, which makes me thing the first click is quitting fragment B which I can't see.
Given that, I believe fragment B is being added behind (or below, if you think of it as a pile) of fragment A. Is there a way to bring it to front?
Inflate Fragment B into a view in the layout of Fragment A (such as a FrameLayout) where the height that view is set to wrap_content, and the Fragment's view will fill whatever size necessary. Use add() when building the FragmentTransaction.

Home screen launcher Android

I have 5 Fragments and of course my .MainActivity.
MainActivity
FragOne
FragTwo
---
FragFive
and then:
activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml
When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load.
My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want.
But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume? What's the best way to go about this?
What you want is not that big a problem. Just load a fragment using SupportFragmentManager in your MainActivity. If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough.
Example for loading it once in MainActivity:
In your onCreate method in MainActivity :
///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();
This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want)
If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne
navigationView.getMenu().getItem(0).setChecked(true); ////Set Navigation drawer manually

Android fragment transition on back navigation to previous fragment

I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().

Android Fragment overlapping each other and showing transparent view

I have an activity with a fragment container layout and in that layout i load a fragment A which has a listview (listview loads data from web). Now on clicking listview item i want to show detail view. For that i am loading another fragment B which is detail fragment. Now to do that first i used
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
But using replace remove the previous fragment A which contain listview and on pressing back it again reloads the onCreateView method of fragment A and its listview data. I want to prevent it from loading fragment A again (or sending call to webserver) on backpress. I want to maintain fragment A state and its listviews scroll position. To do that i used second option which is
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
If i use add() then Fragment B loads on top of Fragment A and Fragment A still visible. It shows both fragments overlapping each other and transparent. I also came across anothe solution which is to use show() and hide() methods. But i dont think that is an efficient way to achieve that. Can anyone suggest an effective solution?

Categories

Resources