I have the following problem:
I have three fragments that I can switch through swiping. The first contains images that are regularly made visible and invisible by an external method. However, I see the change in the visibility of the images only when I leave the fragment by swiping and then swipe back again.
Therefore, the question is which method is called when rewinding back, so that I can call this explicitly if the fragment is visible.
I think this will help when you back on the fragment.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Related
I have an activity with 3 fragments. Every fragment has a question.
When i create the activity I'm calling .add(...) method for the first fragment. For the others i call .replace(...) method with addToBackStack(...) method.
So i have this situation in the backstack:
.add(Frag1)
--> Frag 1
.replace(..., Frag2, ...)
-->Frag2
-->Frag1
.replace(..., Frag3, ...)
--> Frag3
--> Frag2
--> Frag1
Now, when I'm at the third fragment and then go back I'm popping it from the stack. I want to preserve the third fragment so if i go back to the second and then go next to the third i want to have the question answered and not to call again another onCreate().
I want to create a sort of switch of the fragments. Is it possible? Can someone help me?
You can create a fragment once and Show/Hide it depending on your needs.
See hide & show from FragmentTransaction docs
Also see this question
How about you use viewpager instead, you will need to:
Disable paging by swipe, so you can change the page programmatically
Load all pages at once (set setOffscreenPageLimit your pages count), so that each page keeps its state
I've implemented navigation in my Android app as one Activity and many Fragments.
When I open new child Fragment, I call this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction() .addToBackStack(null).replace(R.id.fragment_container, f)
.setBreadCrumbTitle(f.getClass().getSimpleName());
tx.commit()
This works quite well, as old fragment isn't destroyed, so when I go back in the back stack, the old fragment retains the scroll position and everything entered on it.
The problem is, in one Fragment I actually need to pause something when I add another fragment above it. I've thought that onPause() will be called, but it doesn't - I suppose that all other fragments live beneath the ones on top of them.
On the side note, I also think that my app gradually slows down as the user navigates deeper. Shouldn't I be replacing the fragment in the transaction instead, so only one is on the top?
I'm using TabHost and I have a problem. What I want is first tab should be a FragmentActivity and it can switch to another Fragments. This application helps me but I have also questions. In this example, you can go to infinite another Fragments but it's happening by the help of launchNewFragment method and this method is called from xml. But I need to call this method programmatically because when button is clicked, I also want to move some values (such as selected values in dialog) to another Fragment. Or is there another way to solve this problem? I'm waiting your suggestions.
You might want to use Action Bar with Tabs instead of TabHost. You can get information about how to use it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
No matter on the chosen solution, replacing the displayed fragment in an activity is done like this:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new MyFragment());
transaction.commit();
You just have to add this to the button listener and replace MyFragment with your fragment.
I have 2 Fragment's - A and B.
In order to switch from Fragment A to Fragment B I'm using this function:
public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerViewId, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Let's say Fragment A takes the entire screen's area and Fragment B takes only the upper half of the screen.
The problem: When switching to Fragment B, the user is still able to see Fragment A at the lower half of the screen...
How can I hide Fragment A when switching to Fragment B ?
p.s: I don't want to use replace instead of add in the swap function above - I don't want Fragment A's onCreate() to be called each time the user navigate back from Fragment B to Fragment A...
Thanks in advance.
Could you maybe move Fragment A? E.g. slide it down when Fragment B appears? Simply move it outside the screen. Something like what's discussed in this question
Well, without any other option, I've just took care that Fragment B will occupy the entire screen so that Fragment A can't be seen.
More adjustments should have n=been done due to Android's bugs, such as: Google's stupid bug
I'm trying to add animation to my android app, and knew a lot about transition animation between activities, and about fragments. So I have MainActivity with some view (image), and SecondActivity which is empty itself, but contains fragments, which then needs to be used to display some detail information about this image. I've read about postponeentertransition() and it worked. But the problem is, that this fragment in second activity have links(or transitions) to the same kind of fragment.
Let's say in main activity I have set of images of cars, then I click one, second activity is starting and waiting for fragment loading, and then shows animation. And then, in this fragment I have "similar cars" recycler, and choose one of them, and wants, to show its details the same way, but with adding this fragment to the backstack (not replacing). So the problem is that transition name of the view in main activity and big image of fragment are the same (which is obvious), and transition name in fragment's recycler and big image of the next fragment have to be the same too(otherwise app doesn't know how to make the animation). On the other side, fragment (which was added) has to be the same class, but its different instance.
I just want to set up animation without crashing the existing architecture.:) If you have any solutions or advices how to make animation between different instances of one fragment, please help.)
Use setCustomAnimations on the Fragment transaction (see below). It doesn't matter if the instances are from the same class.
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
// To animate the fragment
// NOTE: Must be before replace/add otherwise it doesn't work
transaction
.setCustomAnimations(enter, exit, popEnter, popExit)
.replace(R.id.frame_container, fragment, null)
.commit();