How can I display a Dialog from a PreferenceFragment? - android

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.

Related

How to use android.support.v4.app.Fragment with Activity class?

Ok here is the issue, the native Android Fragment is deprecated.
This will be use in an Unity Android native plugin.
In the old ways you simply create a Fragment class like this:
public class UnityAndroidNativeplugin extends Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
UnityPlayer.currentActivity.
getFragmentManager().
beginTransaction().add(instance, UnityAndroidNativeplugin.LOG_TAG).commit();
}
}
That works fine but I still don't get, if this is deprecated we should use the support fragment library. Ok I will update this.
So when I change to the new version it should be something like this.
public class UnityAndroidNativeplugin extends android.support.v4.app.Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
FragmentManager fragMan = UnityPlayer.currentActivity.getSupportFragmentManager();
//This Fails
}
}
I already know that my main Activity should be a FragmentActivity.
Cannot call getSupportFragmentManager() from activity Check here.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)?
One solution is to override the Unity Main activity but that could have problems when using other plugins. Or maybe using something else than a fragment.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)
Correct.
if this is deprecated we should use the support fragment library
Or, better yet, the AndroidX one, as the Support Library one will become obsolete before long.

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

How to call getSupportFragmentManager() from a fragment?

I want to use a custom date and time picker in my code for that i created a seperate package and put all the date and time picker classes there.
The code is running smoothly if i'm running it from main activity but when i want to call from a fragment it's not recognizing getSupportFragmentManager() method
the call which i'm making from my fragment is :
SimpleDateTimePicker simpleDateTimePicker = SimpleDateTimePicker.make(
"Set Date & Time Title",
new Date(),
this,
getSupportFragmentManager()
);
simpleDateTimePicker.show();
Please help, thank you guys.
You don't.
You need to call getChildFragmentManager() from the Fragment. Attempting to call getActivity().getSupportFragmentManager() will throw an error.
Firstly check your frgaments extends from android.support.v4.app.Fragment or android.app.Fragment .If it is android.app.Fragment then call getActivity().getFragmentManager(); or if it extends from support frgament then call getActivity().getSupportFragmentManager();
looks like SimpleDateTimePicker class is your own class, which accepts only android.app.FragmentManager, not android.support.v4.app.FragmentManager. So you can use that class only with API11 or higher. If you want to use android.support.v4.app.FragmentManager, you need to edit your SimpleDateTimePicker to accept that android.support.v4.app.FragmentManager in constructor
EDIT:
ok, there are 2 different classes: android.app.FragmentManager and android.support.v4.app.FragmentManager. The second one is a compatibility implementation of the first one. Most apps use the second, as it allowed to be used before API 11. And the class from your link uses the first one. There are two ways to fix it:
1st - you start using android.app.FragmentManager instead of android.support.v4.app.FragmentManager. It will cause app work only above api 11.
2nd way is changing code of SimpleDateTimePicker to use android.support.v4.app.FragmentManager. It is more correct way I think.
This is working for me
getBaseActivity().getFragmentManager()
Try it.
This worked for me:
getChildFragmentManager()

Illegalstateexception activity has been destroyed fragmenttransaction.commit()

I have seen a few versions of this question before, but the reasons for this exception were different than my own it seems.
What I am trying to do:
-Main Activity class has a toolbar at the bottom, clicking the buttons will display a series of fragments, one after another.
- A class EditItemFragmentManager, which is instatiated on a button click, and has methods that display specific fragments based on the toolbar button clicked.
I would like to use this manager class I created because it cleans my code up significantly and will make adding more features later helpful.
Here is my EditItemFragmentManager class, I am not sure if extending Activity is a good idea or not, I think that it will put my MainActivity on pause
public class EditItemFragmentManager extends Activity{
//instance variables
public EditItemFragmentManager(){
// initialization of some variables
}
public void editItem(){
editItemSequence();
}
private void editItemSequence(){
EditNameFragment enf = new EditNameFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(editNameFragment, EDIT_FRAG_TAG);
fragmentTransaction.addToBackStack(EDIT_FRAG_TAG);
fragmentTransaction.commit();
}
}
So it blows up when commit(); is called, giving me
java.lang.IllegalStateException: Activity has been destroyed
This is how I am trying to get this fragment from my MainActivity,
#Override
public void onClick(View view) {
EditIteFragmetManager manager = new EditIteFragmetManager();
manager.editItem();
}
I am still learning about the Acvtivity lifecycle in Android. I think my problem is something due to this class extending Activity, which puts my Main on pause, and the FragmentTransaction has nothing to commit to? If so, I need to get the existing instance of my main activity and call it on that? This is where I'm a bit lost, if anyone who understands the lifecycle of Activities/Fragments explain how I could go about implementing this while still having a helper class such as this?
If you're using the SupportFragmentManager, then you need to extend from FragmentActivity, and not just Activity. Also make sure that you imported the Fragment from the v4 support library, and not android.app.
Other than that, you seem to be instantiating a subclass of Activity with "new", which is terrible. Create activities only using Intents.
I solved this issue by moving my manager class to become a private inner class of my main, since they are so tightly coupled. No fragment issues now.

Getting a Fragment from a DialogPreference?

I have a DialogPreference, that I'm trying to get a Fragment that is added to it (Google Maps, if anyone cares). The fragment has an id tag in the XML code, and I'm setting the dialog via setDialogLayoutResource(). However, I'm struggling to get a reference to the fragment (So I can set some settings to it, and get data out from it). I need to get a reference to it before the user can start playing with the data.
What I've tried so far is this:
#Override
protected void onBindDialogView(View view) {
((Fragment) getDialog()
.getOwnerActivity()
.getFragmentManager()
.findFragmentById(R.id.map));
}
This doesn't work because getDialog returns null at this point in the lifecycle. If there's a better way to reference the activity or FragmentManager, I'd love to see it. Thanks for the help!
I managed to get this to work by casting the Context as an Activity.
((Activity) getContext()).getFragmentManager().findFragmentById(R.id.map));

Categories

Resources