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()
Related
When I navigate to an activity using ShowViewModel, it is nicely animated. But when the target is a Fragment it won't. Is there a way to add this as well?
I saw that in native android you would add it to the FragmentTransaction, but since MvvmCross Handles that for us, I assume there is another place to handle that.
The code that handles the fragment transaction is the Show method from the activity implementing IMvxFragmentHost that's responsible for handling the specific Fragment being show. In order to change the animation, you need to use the SetCustomAnimations method when displaying the fragment.
What I usually do is creating a BaseFragmentView class that has enter and leave animations exposed as properties. When displaying the fragments, I can simply use those properties like this:
var transaction = SupportFragmentManager
.BeginTransaction()
.SetCustomAnimations(fragmentView.EnterAnimation, fragmentView.ExitAnimation)
.Replace(targetId, fragmentView)
.Commit();
When using the MvxChachingFragmentView, you can simply override the OnBeforeFragmentChanging method and use the second parameter to add the custom animations you want.
You can see how to implement the IMvxFragmentHost interface by checking the MvxCachingFragmentView class and, if you don't know how to use the new Fragments from MvvmCross 4, refer to this answer
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
is there a way to send some data from an activity to a running fragment?
In my app I'm adding a second fragment over another fragment. I intentionally use the add method instead of the replace method. So now I want to hide my second fragment with
fragmentManager.popBackStack();
and my first fragment reappears. After hiding the second fragment I want to send some data from my activity to the still running frist fragment.
Any idea how to do this? It doesn't work with bundles (put extra), because I don't rebuild the fragment, I just hide the second one!
one simple solution is:
MyFragment oldFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment_place);
fragmentManager.popBackStackImmediate();
MyFragment newFragment = (MyFragment)fragmentManager.findFragmentById(R.id.fragment_place);
newFragment.postData(...);
You can use an EventBus library like this one, it's easy to use and very convenient.
You can use tags on the Fragments when you create them to call them when needed.
getFragmentManager().beginTransaction()
.add(R.id.content, new SomeFragment(), SomeFragment.class.getSimpleName())
.commit();
So above I use the simple name of the class to tag the fragment when I create and add it to the activity.
SomeFragment fragment = (SomeFragment) getFragmentManager().findFragmentByTag(SomeFragment.class.getSimpleName());
And I can call it back when I need it and know it is being displayed like above, now I can call send it data like normal by calling a public method in the custom fragment and passing it the data as a param.
I'm using CalendarDatePickerDialog from BetterPickers in my project,
if i use a FragmentActivity, then i can use the getSupportFragmentManager().
The problem is that i'm trying to add CWAC-Camera as well,
but for that i need to use
getFragmentManager() in a regular Activity which breaks the BettePickers.
any ideas?
Try to extends FragmentActivity and implements CalendarDatePickerDialog.OnDateSetListener
Change your Activity to FragmentActivity.
Use the newInstance(YouActivity.this)
Enjoy!
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.