PreferenceFragment is not added to Activity as expected - android

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

Related

Xamarin: Replace Support.V4.App.Fragment within App.Fragment

How can I replace a frame layout in my App.fragment with Support.V4.App.fragment?
I tried:
FragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_scancontainer, scanFragment).Commit();
and
ChildFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_scancontainer, scanFragment).Commit();
where Resource.Id.fragment_scancontainer is a frame layout in a fragment and scanFragment class inherits from Android.Support.V4.App'
Can anyone please advice ?
You can't have android.support.v4.app.Fragment in android.app.Fragment or vice versa. I'd recommend to refactor your code and have a single fragment type everywhere if possible.
In case of activities, if you need android.support.v4.app.Fragment, the activity must extend FragmentActivity (or better AppCompatActivity) and you must use SupportFragmentManager.

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

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

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

Android viewpager in fragment

Dear ladies and gents,
First of all, please do not mark my question down. If you think that my question is too stupid please let me know and i will edit it or remove.
So my actual question is that I have an activity (extends Activity). In that activity's layout I created FrameLayout where I then attach different four fragments(so each of them extends Fragment. So in one of that fragments I would like to implements swipeable tabs(see screenshot).
I know that it is usually done by implementing viewPager and FragmentPagerAdapter, but I can not do this as if I'm calling getSupportFragmentManager in my fragment it gives my an error. If i am using getActivity().getFragmentManager() it gives me error that android.support.v4.app.fragmentmanager cannot be applied to android.app.fragmentmanager. I have to use android.support.v4.app.fragment in my fragment, because otherwise I will not be able to implement my activity's view(described at the beggining).
Any ideas or suggestions would be very appreciated.screenshot
Make sure you are using Fragment class that you extends your fragments comes from support library. You also need to use FragmentActivity to call method getSupportFragmentManager();
On the other hand, viewpager which is in your fragment need to implemented as usual that you can find on internet except getChildSupportFragmentManager();
This one called "nested fragments".
PS: I am not sure but you can also use AppCompatActivity instead of FragmentActivity. FragmentActivity and ActionBarActivity must be deprecated.
Good luck
You can use getChildFragmentManager() when using Fragments inside other Fragments.
New version of Support Library v4 (Android 4.2) resolve this problem. For do this, simply do constructor of your custom FragmentPagerAdapter like this:
public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
{
super(fragment.getChildFragmentManager());
// write your code here
}
This work because new Android version approve using nested Fragments

Categories

Resources