I have a Fragment called HomeFragment. I initially imported this library with it:
import android.support.v4.app.Fragment;
When I used the FragmentTransaction class in my MainActivity to replace this fragment, I kept getting an error at this point in my code
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container,homeFragment);//***Syntax Error Cannot Resolve Method
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
When I changed the my library to
import android.app.Fragment;
The error went away. Does anybody know why FragmentTransaction does not recognize a Fragment using the support Library when the replace method is called? Also, let's say I did need to use this support library what should I use to replace one fragment with another since FragmentTransaction does not work? Thank you.
You need to use android.support.v4.app.FragmentTransaction with android.support.v4.app.Fragment.
Related
I want to play hide animation on back press.
I have working version of code for following packages:
android.support.v4.app.Fragment;
android.support.v4.app.FragmentTransaction;
code is following:
ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_right);
Fragment fragment = new RegisterFragment();
ft.replace(R.id.sign_in_fragment, fragment);
ft.commit();
But now I am using androidx packages
androidx.fragment.app.FragmentTransaction;
In which case the back press animation does not work. it just removes fragment constantly.
well code is little different but same:
ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_top, R.anim.slide_out_top, R.anim.slide_out_top);
ft.replace(R.id.menu_fragment, menuFragment);
ft.addToBackStack(null);
ft.commit();
R.id.menu_fragment is empty and I do replace but add has same result.
I found one answer which suggests to add tags on fragments but it does not work.
I think it's androidx package problem and I don't know with what to change.
And project does not let me use just same old this package: android.support.v4.app.FragmentTransaction;
What to do or where do I make mistake?
Thanks in advance.
You can implement animation in the onBackPressed Method of your MainActivity.
override fun onBackPressed() {
val fragment = supportFragmentManager.fragments.last()
supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_up,R.anim.slide_down).remove(fragment).commit()
}
check this code..
My_Fragment fragment = new My_Fragment()
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.slide_in_right,
android.R.anim.fade_out)
.replace(R.id.container, home_Fragment_admin).commit()
The problem seems to be the remove function which onBackPressed function is using. Because that my R.id.menu_fragment was empty when I added a fragment, the onBackPressed function uses remove method instead of replace or other.
Which lead me to this question.
I found way out from this (which is suggested in the question's answers) but it's ugly.
Basically What I did was create other fragment and Override onBackPressed so instead of remove function I made it replace by other fragment which is fully hidden by the replace animation.
List<Fragment> fragments = getSupportFragmentManager().getFragments();
Fragment fragment = fragments.get(fragments.size() - 1);
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_out_top, R.anim.slide_out_top).replace(R.id.menu_fragment, exitMenuFragment).commit();
And when entering fragment again I matched animations of enter and exit;
ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_in_top);
very poor execution but only I found.
I want to set custom animations for a transaction between two fragments, but it says that replace in the fragment transaction cannot be applied to InfoFragment(a fragment which extends Fragment), just to android.app.fragment.
infoFragment=InfoFragment.newInstance(latitude,longitude);
getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_right_in,R.animator.card_flip_right_out,R.animator.card_flip_left_in,R.animator.card_flip_left_out)
.replace(R.id.fragment_container,infoFragment)
.addToBackStack(null)
.commit();
Since you're saying your InfoFragment is extending Fragment, the only thing that may be causing the issue is the fragment packages. Make sure your InfoFragment is importing android.app.Fragment only and not from the Support package.
it seems like you forgot to import infoFragment. import the package for infofragment
How to use preference fragment with android.support.v4.app.Fragment?
I tried to use android.preference.PreferenceFragment but I got an error: Wrong 2nd argument type/
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new SettingsFragment());
transaction.addToBackStack(null);
transaction.commit();
SettingsFragment is the preference fragment
What can solve this problem?
By my knowledge PreferenceFragment is not supported in the android.support.v4 library.
You can however use PreferenceFragmentCompat from the support-v7 library.
If it really has to work with the support-v4 library, I would recommend adding the following project as a library project to your application as suggested by this old thread.
https://github.com/kolavar/android-support-v4-preferencefragment
I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
I have a actionbar tab setup with fragments for each of the pages. (extended default example from wizard).
I'm attempting to create a new fragment and make it the current fragment, with the other hidden
ConfigDetailSectionFragment configDetailSectionFragment = new ConfigDetailSectionFragment();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_main_config, configDetailSectionFragment);
transaction.addToBackStack(null);
transaction.commit();
the code shows the new fragment but seems it is transparent and I can see the fragment below.
i'm probably forgetting to do something... what am i forgetting to do or am missing?
Adding an android:background="#FFFFFF" fixed the issue, per beworker. Thanks!