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
Related
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 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
I'm trying to generify dynamic fragment instantiation:
private <F extends Fragment> void addFragment(F fragment) {
fragment.setArguments(new Bundle());
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.add(R.id.container, fragment);
tx.submit();
}
However, the compiler rejects tx.add(R.id.container, fragment) because there's no method signature for add(int, F)!
How do I make the add() method understand that F is indeed a Fragment?
Thanks in advance :)
That is because you imported a wrong Fragment in your class that it is not compatible with the fragment argument of the add method.
since you used a getFragmentManager() that means you need to use the non support library for the fragment to enable the generic to be compatible with the add method.
Make sure that your import is like this:
import android.app.Fragment;
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'm trying to bring up a DialogFragment when I tap a Preference in a PreferenceFragment. Unfortunately, when I call getFragmentManager() in DialogFragment.show() I receive the following error:
Cannot resolve method 'show(android.app.FragmentManager, java.lang.String)'
The problem is that I can't seem to refernece android.support.v4.app.FragmentManager from this fragment. The activity in charge of this fragment extends from FragmentActivity, but obviously that's not enough. I tried calling getSupportFragmentManager() as well, but that gave me this error:
Cannot resolve method 'getSupportFragmentManager()'
How can I make this work?
Here's some code:
gPrefAcknowledgements.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener()
{
#Override
public boolean onPreferenceClick(Preference preference)
{
DialogAcknowledgements dialogAck = new DialogAcknowledgements();
dialogAck.show(getFragmentManager(), "acknowledgements");
return true;
}
});
Building on Steve's answer, I also need to set up the DialogFragment to import from android.app.DialogFragment (instead of android.support.v4.app.DialogFragment). The show() function in that library asks for an android.app.FragmentManager, which I can provide via a call to getFragmentManager() as I did within the code I posted in the question.
Try this:
dialogAck.show(getActivity().getFragmentManager(), "acknowledgements");
You can always call the Activity which holds the Fragment equal of which type it is.
EDIT:
I was wrong, the PreferenceFragment isn't included in the Support Library and is only available in Android 3.0 and higher. This post could help to deal with this scenario.