Replace in FragmentTransiction cannot be applied to InfoFragment - android

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

Related

getSupportFragmentManager: incompatible types

I followed this tutorial: https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer
Now Im at this point:
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
My Problem is that this line..
FragmentManager fragmentManager = getSupportFragmentManager();
show me an error : incompatible types. Required android.app.FragmentManager Found: android.support.v4.app.FragmentManager.
I saw some posts but they doesn't work for me.
I extend my class with AppCompatActivity, tried FragmentActivity but this doesn't work.
If I change FragmentManager to android.support.v4.app.FragmentManager, the error disappear but then
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); shows: Wrong 2nd argument type.Found:'android.app.Fragment',required'android.support.v4.app.Fragment'
Pls help me :/
Change
import android.app.Fragment;
import android.app.FragmentManager;
to
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
in every class. you are facing problems because in your Fragment creation class you are using support v4 fragment and in your MainActivity class you are inflating as a simple fragment.
You also need to change getFragmentManager() to
getSupportFragmentManager(), and make sure they're extending a FragmentActivity class.
Hope it will help you.

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.

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