I have one dialog with list of item.
on click of each item i push one fragment.
I want that fragment to load with animation.
I knew how to do animation from fragment to fragment
but help me to apply animation when i have only one fragments
This is what i already tried, this helps to apply transition between two fragments.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
DetailsFragment newFragment = DetailsFragment.newInstance();
ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");
// Start the animated transition.
ft.commit();
Required common method for each item click where different fragment push.
You have to put common method in code.
Method code
public void pushFragments
(Fragment fragment, boolean shouldAnimate, boolean shouldAddinbackstack, String tag) {
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fragment, tag);
if (shouldAddinbackstack) {
//add fragment in backstack entry
ft.addToBackStack(tag);
}
if (shouldAnimate) {
//fragment animated
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
}
ft.commit();
}
use method on item click e.g
DetailsFragment newFragment = DetailsFragment.newInstance();
pushFragments(newFragment, true, true, "detailsFragment");
Related
I have implemented fragments in my application. Here my code for swiching fragment in fragment_container.
private void switchFragment(Fragment fragment, boolean isAddToBackStack, String tag)
{
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment, tag);
if (isAddToBackStack)
ft.addToBackStack(tag);
setCurrentTopFragment(Integer.parseInt(tag));
ft.commit();
}
I have 4 fragments A,B,C and D and for switching between this fragmnets I am using above method. I have A,C,B in my backstack. If again I switch to fragmnet A, my backstack is like A,C,B,A. What I actually want is If I swich to A again I want backstack sequence like this C,B,A. Means Remove old instance from backstack and add new to it.
First get the back-stacked Fragment by id which you need to remove:
Fragment fragment = getSupportFragmentManager().getFragment(new Bundle(), TAG_KEY)
or there are several methods getBackStackEntryCount(), getBackStackEntryAt. After getting the fragment which you need to remove. Remove it from the fragment back-stack.
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
Then you can add a new fragment Done :)
I have 2 fragment on one fragment activity. I have 2 fragments say new and pending but I am having problem while switching from one fragment to another while tapping of button. When I am on pending tab and then click on new tab then sometimes it shows two loader and then it still shows pending page. My both fragments are android.support.v4.app.Fragment. I am attaching screenshot of the problem while I am tapping on new tab and still showing pending tab with two loader.
code for switching between fragment
#Override
public void onClick(View v) {
if (v.equals(neww))
{
Fragment newpage = new NewPageActivity();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, newpage);
ft.commit();
}
else if(v.equals(pending))
{
Fragment pendingFragment = new PendingPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, pendingFragment);
ft.commit();
}
}
now screenshot of problem
Try this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelay, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
I have added 3 fragments to my Activity with
String name = "fragment1"; // and ..2 and ..3
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, name);
fragmentTransaction.addToBackStack(name);
fragmentTransaction.commit();
The last added (third) Fragment now is visible on top.
Now I want to resume to the first added Fragment. But how?
I can find this Fragment with
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment firstFragment = fragmentManager.findFragmentByTag("fragment1");
If I call fragmentManager.getFragments() I still can find all three Fragments.
How to bring firstFragment back to top, make it visible again?
You can hide your 2nd and 3rd fragment and make your 1st fragment visible. So you'll have the effect that first fragment is shown on top and others are invisible.
solution:
Use the FragmentTransaction's show and hide method. Firs you need to find all the fragment and call the FragmentTransaction to show and hide 2nd and 3rd fragments.
Here's how I do it in my app when I want to switch to fragment:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isAdded())
{
transaction.show(fragment);
}
else
{
transaction.replace(R.id.container, fragment);
}
transaction.addToBackStack(null);
transaction.commit();
Note the use of show.
It ended up with this:
/**
* #param tag name of the fragment to resume and to bring to top
* #return true if fragment has been found
*/
private boolean bringFragmentToTop(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (Fragment f : fragmentManager.getFragments()) {
if (f == fragment)
fragmentTransaction.show(f);
else
fragmentTransaction.hide(f);
}
fragmentTransaction.commit();
return true;
}
return false;
}
I am developing a simple android app only for tablets and using android 4.0. My application have the main screen like as follow:
Oncreate() of Main Activity I am adding Fragment A in the main.xml using following code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment imageFragment = new ImageFragment();
ft.replace(R.id.fragment_container, imageFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
This fragment A just have only a Image view which is clickable. Now I want that when user click on Image view then another fragment (Fragment B) should call and it replace the image view. The Fragment B have a VideoView which play the video.
So My second screen should be like as follow:
My problem is I am not gettting "How to call second fragment from the first one with in main screen activity?"
I can use different activities but I do not want to do so and just want to run this using fragments.
Please guide me.
This is the simplest way:
1) Inside YourActivitycreate a method:
public void goToSecondFragment(){}
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment secondFragment = new SecondFragment();
ft.replace(R.id.fragment_container, secondFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
2) In your first fragment, when you want to replace it, call:
YourActivity activity = (YourActivity) getActivity();
activity.goToSecondFragment();
You can do the same using below:
Adding a fragment with maintaining a back stack
FragmentManager supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
}
});
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("fragmentTag");
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
And Replacing a fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
Should work for you.
I want to do the following. There are two fragments first and second. Necessary make the transition between them. When I go from first fragment in the second, first stored in the stack. When I click the Back button the second fragment is removed and returned first fragment from the stack. Again I can not go in the second fragment - it has been deleted. How can I solve this problem?
In main activity (callback for Fragment1):
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
Fragment2 newFragment2 = (Fragment2) getFragmentManager().findFragmentByTag("frag_2");
ft.replace(R.id.main, newFragment2);
ft.remove(newFragment1);
ft.addToBackStack(null);
ft.commit();
}
Fragments I added dynamically:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.main, new Fragment1(), "frag_1");
ft.add(R.id.main, new Fragment2(), "frag_2");
ft.commit();
I solved this problem :). I hide first fragment and add transaction to the back stack. When I click button Back I return to fragment
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
ft.hide(newFragment1);
ft.addToBackStack(null);
ft.commit();
}