setTargetFragment for android.support.v4.app.Fragment? - android

I am trying to pass info from a DialogFragment to it's hosting Fragment, but the problem is I am using the android.support.v4.app.Fragment instead of the regular android.app.Fragment package for my Fragments.
This code gives me an error:
messageResponseDialog.setTargetFragment(this, 0);
setTargetFragment is expecting a Fragment and not a support.v4.Fragment.
How can I do implement a callback from the DialogFragment to the hosting Fragment using the android.support.v4.app.Fragment?

You are trying to set a target fragment from a DialogFragment that is not from the android.support
solution:
change your DialogFragment to android.support

Related

Xamarin: Replace Support.V4.App.Fragment within App.Fragment

How can I replace a frame layout in my App.fragment with Support.V4.App.fragment?
I tried:
FragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_scancontainer, scanFragment).Commit();
and
ChildFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_scancontainer, scanFragment).Commit();
where Resource.Id.fragment_scancontainer is a frame layout in a fragment and scanFragment class inherits from Android.Support.V4.App'
Can anyone please advice ?
You can't have android.support.v4.app.Fragment in android.app.Fragment or vice versa. I'd recommend to refactor your code and have a single fragment type everywhere if possible.
In case of activities, if you need android.support.v4.app.Fragment, the activity must extend FragmentActivity (or better AppCompatActivity) and you must use SupportFragmentManager.

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()

Launch a android.app.fragment in support.v4.fragment

I have two fragments in my viewpager: An android.app.fragment and a support.v4.app.fragment.I know that viewpager does not support a android.app.fragment.So is there any way to launch a android.app.Fragment from a support.v4.app.fragment?
I think you can call getActivity() from your support.v4.app.fragment and then call .getFragmentManager() instead of .getSupportFragmentManager() and then .add() or .replace()
BUT
I suggest you to use support.v4.app.fragment for all of your fragment and use an AppCompatActivity for your activity.

how to start an activity inside fragment viewpager

I'm creating a swipeable Sherlock tab. I used this tutorial to do it:
Android ActionBarSherlock ViewPager Tabs Tutorial
However when I want to start an activity from inside of a fragment (for example FragmentTab1) I need context to create intent and start activity.
I'm in the Fragment class and don't have access to context!
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
What should I do?!
Sherlock Fragments works with getSherlockActivity() instead of getActivity()
You should try using
getSherlockActivity().startActivity(...)
I'm in the Fragment class and don't have access to context!
Yes, you do. Call getActivity() to return the Activity that is hosting this fragment. Activity inherits from Context.
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
So? That does not somehow magically cause the getActivity() method to vanish.
For some reason, you are using ActionBarSherlock, which has been deprecated for about 20 months by its author. If your fragment is a SherlockFragment, you will want to call getSherlockActivity() instead.

Categories

Resources