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
Related
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.
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
I am using Eclipse and when I create Blank activity with Fragment it is using
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new placeholder()).commit();
}
in the onCreate method of MainActivity which needs import app.fragment and I need to use loginbutton.setfragment(this) in Placeholder class needs import android.v4.app.Fragment and I cannot import both of them together. somebody please help
It is advised to use Fragment or v4.Fragment thru out the project.
In your case import android.v4.app.Fragment and use getSupportFragmentManager instead of getFragmentManager
I want to retain the instance of an object on configuration changes using Fragments. And I want to support older versions of android using support library. So I extended FragmentActivity like this.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class test extends FragmentActivity {
#Override
public void onCreate(Bundle state){
super.onCreate(state);
setRetainInstance(true);
}
}
But when I do this, Eclips complains that there is no such a thing as setRetainInstance. But when I change the FragmentActivity to Fragment every thing is OK.
what am I missing?
setRetainInstance(Boolean) is a method in Fragment class. not of FragmentActivity class...
and There is a support library for Fragments in Android SDK.
see at android-sdk/extras/android/support/v4/android-support-v4.jar
I trying to extends my main class from TabActivity ... I wrote the code as bellow :
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabAndListView extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
.
.
}
but I have a worning that the type TabActivity is deprecated !!!!!
Have any one any Idea about this ???
thank you in advance , Fadel
Please see the official document.
This class was deprecated in API level 13. New applications should use
Fragments instead of this class; to continue to run on older devices,
you can use the v4 support library which provides a version of the
Fragment API that is compatible down to DONUT.
See also:
"The type TabActivity is deprecated" For app tab
https://stackoverflow.com/questions/12600207/the-type-tabactivity-is-deprecated-help-to-use-in-older-device
Android: TabActivity deprecated, use Fragments?
Or if you have a more specific question, you can add more info.