getSupportFragmentManager: incompatible types - android

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.

Related

androidx. FragmentTransaction animation does not work on backpress

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.

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

fragment manager in navigation drawer error

I am using Fragment manager in navigation drawer in android studio.I typed the following code as
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(content_frame, fragment).commit();
It shows red line below fragment in above code.
The screenshot is also uploaded.enter image description here
Change getFragmentManager() for getSupportFragmentManager()and import supportV4

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.

v4 getFragmentManager with Activity - Incompatible types

I have a simple activity which runs as expected.
import android.app.Activity;
import android.app.FragmentManager;
// import android.support.v4.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager(); // Activity
}
}
I then replaced
import android.app.FragmentManager;
with
import android.support.v4.app.FragmentManager;
so I could support my older devices.. However, this reports an error:
Incompatible types.
Required: android.support.v4.app.FragmentManager
Found: android.app.FragmentManager
What am I doing wrong here?
The popular solution I found is to use getSupportFragmentManager() instead, but this only works for ActionBarActivites [edit - see answers] and FragmentActivities.
cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager
The other relevant solution points to using a FragmentActivity instead, but this appears to have the same legacy problems.
The method getFragmentManager() is undefined for the type MyActivity
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager();
}
}
I'm pretty sure the solution to this will be on SE already, but it isn't easy (for me) to find. The minimal example should help other people with understanding it, too.
I'm fairly new to Android.
In your first case if you use getSupportFragmentManager() you need to extend FragmentActivity or extend ActionBarActivity (extends FragmentActivity) as FragmentActivity is the base class for support based fragments.
In your second case you need to use getSupportFragmentManager() instead of getFragmentManager().
Fragments were introduced in honeycomb. To support fragments below honeycomb you need to use fragments from the support library in which case you need to extend FragmentActivity and use getSupportFragmentManager().
use getSupportFragmentManager() instead, but this only works for ActionBarActivites.
Wrong, it should work in your FragmentActivity, that FragmentActivity is in your support package, when you are supporting older device then all your import from activity,fragment, fragment managers, etc. must have android.support.v4. to specify that you are using the support package. without using so will result to incompatible compile time error.
What am I doing wrong here?
You are combining support package with non support package which result to incompatible compile time error, as I said above.
for Kotlin guyz u dont have to change to not v4
use below to get the activity as long as its the parent
(activity as MainDrawerActivity)
or
((YourActivityName)getActivity());
For use in Java Dialog:
- import android.app.FragmentManager;
- FragmentManager fragmentManager;
For Kotlin :
- (activity as? YourActivity)?.fragmentManager

Categories

Resources