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

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.

Related

Kotlin: FragmentManager is showing error when called from Fragment [working with Adapter]

AdapterClass:
class TeamsAdapter(
supportFragmentManager: FragmentManager,
teams_list: MutableList<Team>,
match_id: String
) : FragmentPagerAdapter(supportFragmentManager) {code}
Fragment Class:
lateinit var teamsAdapter: TeamsAdapter
teamsAdapter = TeamsAdapter(supportFragmentManager, match?.teams!!, matchId)
Error: Unresolved reference: supportFragmentManager
But the entire code works fine when inside an Activity instead of Fragment Class.
Solved by casting activity if you are using fragment then cast with context
fragmentManager = (activity as FragmentActivity).supportFragmentManager
you can use parentFragmentManager or childFragmentManager for using fragment manager inside of a fragment. There is no need to use any sort of casting for using a fragment manager.
parentFragmentManager or getParentFragmentManager()
Return the FragmentManager for interacting with fragments associated with this fragment's activity. Note that this will available slightly before {#link #getActivity()}, during the time from when the fragment is placed in a {#link FragmentTransaction} until it is committed and attached to its activity.If this Fragment is a child of another Fragment, the FragmentManager returned here will be the parent's {#link #getChildFragmentManager()}. #throws IllegalStateException if not associated with a transaction or host.
childFragmentManager or getChildFragmentManager()
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
I just read that you are learning something new, so I suggest it is better to use the best practices. Casting sometimes generates errors and crashes, so I suggest you use these.
When your fragment contains a ViewPager that uses fragments, you must always use the childFragmentManager for that ViewPager as that's what allows FragmentManager to properly nest those fragments and ensure that their state is properly saved and restored.
You should use supportFragmentManager if you are supporting device below API 14, otherwise just use fragmentManager

Replace in FragmentTransiction cannot be applied to InfoFragment

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

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

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.

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

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

Categories

Resources