I've followed instructions from dmanargias answer here: Android Fragments and animation
The animations themselves work, however the initial animation when adding a fragment is doing something strange. The initial fragment appears to be replaced with the new fragment before the animation is started.
e.g. One would expect an animation of
A <- B (B sliding from right to cover A)
However as soon as the action starts A instantly becomes B and you get an animation of
B <- B.
When popping the stack you get a correct animation of A -> B (B sliding away revealing A)
This is the code that adds a fragment:
CategoryFragment newFragment = CategoryFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
fragmentTransaction.replace(R.id.fragment, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Any ideas why this would happen and if there's a way to fix it?
Try this:
final CategoryFragment newFragment = CategoryFragment.newInstance();
final View container = findViewById(R.id.fragment);
container.postDelayed(new Runnable() {
#Override
public void run() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
transaction.replace(container.getId(), newFragment).commit();
currentFragment = cardFragment;
}
}, 0);
I was having exactly the same problem, but with different animations.
Check that android:shareInterpolator is not set to true in your xml animations.
Related
I'm creating a fragment which is supposed to act like a menu. I have successfully inflated it to the activity where I wanted it to be, however I now I realise that I cannot close the fragment. Furthermore I am able to scroll the contents of the activity which the fragment is placed over. How can I edit my code in such a way that the fragment will close after an action on the activity is detected, or one of it's contents is clicked?
I created the fragment by simply adding a fragment via new -> Fragment -> Fragment(Blank). I have not touched any of the code and have initialized the fragment like so in a on click:
findViewById(R.id.Menu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_left);
MenuFragment menuFragment = new MenuFragment();
fragmentTransaction.replace(android.R.id.content, menuFragment);
fragmentTransaction.commit();
}
});
This is what it looks like, ignore the horrendous design.
Here's my Flow/Structure:
Activity
Fragment
Same Fragment within it.
I have overriden removeCurrentFragment() method which does go back functionality by removing Fragment by ID as follows -
#Override
public void removeCurrentFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.fragment);
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
It seems, when this happens, as ID of both fragment is same, it creates blank view.
If more details are required, please feel free to ask in comments. I will edit question as per required details.
I thought it was related to remove fragment, But I made new fragment with separate XML, still I am having the same issue.
I have parent fragment with pagination (as I need horizontal page scroll) in it have the child fragment, when clicking on child fragment button new fragment is replaced so when it is removed the X and Y of the child fragment is also disturbed.
here is the before and after screenshot of the x and y axis of child fragment.
Before
After
can any one suggest what could be the issue?
You have to use a tag to load the Fragment, and then use that same tag again to remove it.
For example, to add the Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragmentManager.popBackStack(tag, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.setCustomAnimations(R.anim.enter, R.anim.exit);
ft.add(containerId, fragment, tag);
ft.addToBackStack(tag);
ft.commit();
To remove it, again use the same tag:
if (fragmentManager.findFragmentByTag(currentTag) != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(fragmentManager.findFragmentByTag(currentTag));
ft.commit();
}
I have this problem in an app, where that method below doesn't always effectively hide the currently visible fragment (contentFragment) and shows the new one (fragment). Basically, sometimes, it does the complete opposite of what it's supposed to do, that is, the current fragment remaining visible, and the new one not showing at all. And what is worth, is that I can't always replicate when it happens.
I'm really at wits' end here. Anyone got any ideas?
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(contentFragment);
transaction.commit();
fragmentManager.executePendingTransactions();
contentFragment = fragment;
}
Your contentFragment sometimes do not have the value you are expecting, try to retrieve the current fragment to ensure you are really hiding the fragment before your new transaction.
Modify your addContentFragment(GenericFragment fragment) as the follows:
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.ContentLayout);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(currentFragment);
transaction.commit();
}
So, I have this problem: I am initializing a fragment - AddingTaskFragment
here's code:
Initializing AddingTaskFragment
private void initFragment()
{
// Get fragment manager
FragmentManager fm = getSupportFragmentManager();
// Begin transaction
FragmentTransaction ft = fm.beginTransaction();
// Create the Fragment and add
addingTaskFragment = new AddingTaskFragment();
ft.add(R.id.fragment_task_type_container, addingTaskFragment, "addTaskFragment");
// ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
// Commit the changes
ft.commit();
}
And it works fine
But then, I call some event, that replaces this fragment with other one(AddingScheduleFragment).
Replacing fragment
#Override
public void onScheduleTypePick()
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// Create the fragment and attach book index
addingScheduleFragment = new AddingScheduleFragment();
// Replace the book list with the description
ft.replace(R.id.fragment_task_type_container, addingScheduleFragment, "addScheduleFragment");
ft.addToBackStack(null);
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.commit();
}
And when I'm poping my previous fragment(AddingTaskFragment) from the stack. All of my EditViews are gaining focus.
Returning previous fragment
#Override
public void onTaskTypePick()
{
getSupportFragmentManager().popBackStack();
}
What can be wrong? Why is this happening? Thanks for answers.
Important: When I replacing AddingTaskFragent with new object everything works great.
So, I've solved the problem by myself. Rather found the similar one.
afterTextChanged() callback being called without the text being actually changed
When the fragment was attached second time(because of .replace() method) it was restoring the previous state of the views. And then all textChangeListeners triggered.
I implement a BottomNavigation view for android and I have some fragments to show as BottomNavigation pages. According to the Google Material Design Guide Lines I want to show fragments with cross fade animation.
By touching BottomNavigation's items my ViewpPager change the fragments with default slide animation.
I read some solutions in this and this. but these aren't really fade animation and I couldn't set fading duration.
So is there any way to set an animation on changing ViewPager's tabs ?
Finally, I found my answer.
I changed the ViewPager with a layout to keeps my fragments(a frame layout). Then I added fragments into a fragmentTransaction.
By touching an item on BottomNavigation, current fragment hides and new fragments goes to shown with a fade animation defined in fragmentTransaction.
and this is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
addFragmentsToManager();
}
private void addFragmentsToManager() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.add(R.id.flContent, tripFragment, tripFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, notificationFragment, notificationFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, searchFragment, searchFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, profileFragment, profileFragment.getClass().getSimpleName());
fragmentTransaction.hide(tripFragment);
fragmentTransaction.hide(notificationFragment);
fragmentTransaction.hide(searchFragment);
fragmentTransaction.hide(profileFragment);
fragmentTransaction.commit();
}
private void changeTab(int position) {
Fragment fragment;
switch (position) {
fragment = .....// get framgnet from position
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.hide(prvFragment);
fragmentTransaction.show(fragment).commit();
prvFragment = fragment;
}
There is a problem with adding and hiding fragments.
When the application is idle and the phone goes to sleep mode if you turn back to the application all fragments shown in activity and you see all layouts in one.