Preference Fragment with android.support.v4.app.Fragment - android

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

Related

fragment popbackstack behaviour broken in 25.1.0 and 25.1.1

Since support version 25.1.0 and the most recent 25.1.1 I got strange behaviour with fragment replacing/adding.
There have been issues reported for 25.1.0 Android - fragmentTransaction.replace() not works on support library 25.1.0
But now in 25.1.1 i got similar issues. To reproduce the behaviour i created sample app which you can find at https://github.com/holoduke/fragmenttest
It is basically one Activity with a fragment container. A couple of fragments are available which will be dynamically replace each other by pressing a button. We start with adding FragmentA from the mainActivity itself.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = new FragmentA();
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
f.setRetainInstance(false);
ft.replace(R.id.fragmenttarget, f);
ft.addToBackStack(null);
ft.commit();
All good Works fine. in both 25.0.1, 25.1.0 and 25.1.1
Now in fragmentA there are 3 buttons which will all replace the current fragment with either fragmentA, fragmentB or fragmentC
the code for adding fragment B and C is almost the same as fragment A except we have not defined:
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
when fragment B or C is added the following code is executed:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = new FragmentB();
f.setRetainInstance(false);
ft.replace(R.id.fragmenttarget, f);
ft.addToBackStack(null);
ft.commit();
Still all good in both 25.0.1, 25.1.0 and 25.1.1.
If you add fragmentB and C a couple of times the fm.getBackStackEntryCount() is increasing. Thats good.
Now the weird part.
We want to add FragmentA with popStackImmediate (to clear history)
Here the behaviour of both 3 support versions are going mad.
Lets say that you execute the following bavhiour in all 3 versions:
start app
replace with fragment B
replace with fragment C
replace with fragment B
replace with fragment C
replace with fragment A
in 25.0.1 everything works out fine. the backstack is cleared and onCreateView and ActivityCreated is called in FragmentA.
in 25.1.0 somehow after replacing with FragmentA the onCreateView and ActivityCreated are called 2 times. Not good.
in 25.1.1 its even worse. after replacing with fragmentA, for all views in the backstack the onCreateView and ActivityCreated are called. Now Thats funny right :)
Just try out my sample app and look in the logcat. change the support version in app.gradle file to see the differences.
I would be glad if someone is able recognise this issue as well, so we can find a way to overcome or even solve this issue.
Well, I faced with the same problem and found a solution by comparing 25.0.1 -> 25.1.1 FragmentManager.class. Try to use setAllowOptimization method of FragmentTransaction.

How to move from "android.app.Fragment" to "android.support.v4.app.Fragment"

I am working on Facebook integration for my application. All of the fragments I am currently using in my application, as per client requirement, is
android.app.Fragment. Now, the Fragment we created for Facebook is android.support.v4.app.Fragment. If we convert it into android.app.Fragment, the functionality loginButton.setFragment(this); shows error.
My question is, How can I move from android.app.Fragment to
android.support.v4.app.Fragment as this piece of code is giving error:
getActivity().getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,new MainFragment())
.addToBackStack(null)
.commit();
The error it shows is:
Wrong 2nd argument type. Found:
'com.nicklodean.nicklodean.fragment.MainFragment', required:
'android.app.Fragment'
Please suggest how can I sort out this issue.
First, you need in your Gradle dependencies
compile 'com.android.support:appcompat-v7:XX.Y.Z'
For the respective SDK XX.Y version you have.
(Really only need the v4 library, but v7 shouldn't hurt)
I can't help you with loginButton.setFragment(this) because I do not know what that is doing... But, you of course need to change the class parameter/import of your loginButton class.
Assuming you are using AppCompatActivity or FragmentActivity, you need to use getActivity().getSupportFragmentManager().
Personal opinion: You should be having callbacks to the Activity from the fragments to manage the FragmentManager (as shown here), not directly pulling it from the Fragment classes using getActivity()
just add these dependencies
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile "com.android.support:appcompat-v7:18.0.+"
}
I hope this helps
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment oldFragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_TAG);
if (oldFragment == null) {
ft.add(yourfragment,YOUR_FRAGMENT_TAG);
ft.commitAllowingStateLoss();
} else if (!oldFragment.isAdded()) {
ft.remove(oldFragment);
ft.add(yourfragment,YOUR_FRAGMENT_TAG);
ft.commitAllowingStateLoss();
}
You simply must use the android.support.v4.app.FragmentManager with any android.support.v4.app.Fragment, and the android.app.FragmentManager with any android.app.Fragment. There is no way around this.
If you need to use both versions of the Fragment class together (in a single Activity), you will have to manage the fragments within each FragmentManager separately. Take a look at this answer to see how to do so.

Replace in FragmentTransiction cannot be applied to InfoFragment

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

Importing Fragment Library

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.

PreferenceFragment is not added to Activity as expected

I'm trying to implement PreferenceFragment according to the API in the Google docs, copying and pasting.
Now here it says:
You can then add this fragment to an Activity just as you would for
any other Fragment.
However, there is a compile-time arror at this line:
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
saying that:
replace (int, android.support.v4.Fragment) in FragmentTransaction
cannot be aplied to (int, PrefsFragment)
where my class PrefsFragment extends PreferenceFragment.
What am I doing wrong?
Currently `PreferenceFragment' is not part of support library in Android, that's why you can't use it there, I found a library that solved that problem. You can find it here just add it to your project and you are good to go

Categories

Resources