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.
Related
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.
I'm using AppCompatPreferenceActivity, that is an Activity which extends PreferenceActivity and has an AppCompatDelegate. I want to add a headless fragment to this Activity, but I can't call to getSupportFragmentManager...
Is there a way to add fragments to a PreferenceActivity using AppCompatDelegate?
The only way to call getSupportFragmentManager is from a FragmentActivity or something which derives from it. PreferenceActivity derives from Activity which cannot use Fragments.
You should look into using a PreferenceFragment instead.
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.
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
I am trying to call a Fragment class from Activity using Intent. Is it possible to implement. Please provide your views.
Fragment needs to be hosted by a FragmentActivity, you can't add a fragment via an Intent.
You need to create a FragmentManager to add your fragment in the FragmentActivity (or call another FragmentActivity via an Intent and add your fragment on it).
See this topic for more information: Add a Fragment to an Activity at Runtime.
Fragment TargetFragment=new target_fragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content,TargetFragment);
transaction.addToBackStack(null);
transaction.commit();
Intent can not be applicable to Activity to Fragment So there Is Another method
getSupportFragmentManager().beginTransaction().replace(R.id.container,new DashBoardFragment()).commit();