How to invoke getSupportFragmentManager() or getFragmentManager() in Activity? - android

I have some questions about DialogFragment in Android.
I made CustomDialogFragment that extends DialogFragment and implemented onCreateDialog() function in it.
Now I want to show the Dialog in my Activity.
CustomDialogFragment cdf = new CustomDialogFragment();
cdf.show(getSupportFragmentManager(), "myCustomDialog");
However I can't invoke getSupportFragmentManager() in my Activity.
so my questions are:
Could you please tell me how to solve this problem?
Do I have to use FragmentActivity instead of Activity?
What's the differences between FragmentActivity and Activity?

Use AppCompatActivity instead of Activity to use getSupportFragmentManager() in Your Activity
and use getFragmentManager() or getActivity().getSupportFragmentManager() in fragment

Related

getActivity() from Fragment is FragmentActivity not AppCompatActivity?

I have a BottomnavigationView in an AppCompatActivity that navigates between Fragments. For one of the Fragments, it's a MasterDetailFragment where you can search in that fragment and it will filter the data in the MasterFragment and you can click on the list to view the DetailFragment (all of this happens while the user is on one of the tabs in BottomNavigationView). I'm trying to use the new arch ViewModel to share the data between Master and Detail fragments: https://medium.com/#bharathkumarbachina/sharing-data-between-fragments-34afb6553380. It says to use getActivity() and share the viewmodel that way, but getActivity() is not getting the AppCompatActivity but instead is getting a FragmentActivity. Do I need to make a new MasterDetailFragmentActivity?
AppCompatActivity is a subclass of FragmentActivity. You can cast the FragmentActivity to AppCompatActivity after doing "instance of" check.
https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

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.

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.

Android: How to call Fragment class from activity using Intent

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

show dialog without FragmentActivty

This may be rather obvious, but I cannot get it.
I am trying to use the suggested way of creating dialogs, by extending DialogFragment. Now, the problem is, I don't know how to invoke it. Documentation states:
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
but my Activity does not extend FragmentActivity (and due to the design of my application, it cannot extend it), so getSupportFragmentManager() cannot be invoked.
Any workaround for this?
I would like to skip the deprecated way of creating a dialog.
The answer is no.
The FragmentActivity (and it's variants) are the ones that support the FragmentManager and the Fragments.
Common solutions to what you might believe "and due to the design of my application, it cannot extend it"
If you're using a ListActivity, just put a list in your layout.
If you're using a MapActivity, switch to the new map API.
If you're using a TabActivity, that's deprecated, you should use Fragments.
For example you can create a class that extend DialogFragment and invoke it like this:
_dialogFiltre = FragmentDialog.newInstance(R.string.tle_dialog_filter, this);
_dialogFiltre.setValidDialogListener(this);
_dialogFiltre.setCancelable(false);
_dialogFiltre.show(getSupportFragmentManager(), null);
The method new instance :
public static DialogFilter newInstance(int title, Context context) {
FragmentDialog dialog = new FragmentDialog();
Bundle args = new Bundle();
args.putInt("title", title);
dialog.setArguments(args);
dialog._context = context;
return dialog;
}
If you are not in a FragmentActivity i don't see any solution. Why does your application design does not let you use FragmentActivity ?
well you can directly invoke getSupportFragmentManager(); on FragmentActivity but not on Fragment itself. So to invoke getSupportFragmentManager(); what you can do is first get FragmentActivity and on that call getSupportFragmentManager();. So as you're getting FragmentActivity, you can call this function like:
getActivity().getSupportFragmentManager();
here getActivity() returns FragmentActivity associated with your Fragment.
what you have done is
1. First you have created instance of your custom DialogFragment class.
2. you're showing that dialog.
Now as you've already created an instance of custom DialogFragment why not use that to retrieve support for fragment manager. So what you can do is:
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(newFragment.getActivity().getSupportFragmentManager(), "missiles");
what this will do is you're getting FragmentActivity associated with newFragment and on that FragmentActivity you're calling getSupportFragmentManager();.
Unfortunately I have not tested it but this might work. Try it and let us know.

Categories

Resources