I'm new to the concept of Fragments
In the video I was watching, they used this piece of code:
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.my_container);
if(fragment == null){
fragment = new FragmentMain();
fragmentManager.beginTransaction()
.add(R.id.my_container, fragment)
.commit();
}
To create a Fragment via java.
In findFragmentById they passed in a FrameLayout(my_container) rather than a Fragment. I read in the docs that you can pass in a container id, but it confuses me how it will initialize it as a Fragment. How does this work?
Should I use FragmentManager? I read in the docs that it's deprecated.
Thanks!
In findFragmentById they passed in a FrameLayout(my_container) rather than a Fragment. I read in the docs that you can pass in a container id, but it confuses me how it will initialize it as a Fragment. How does this work?
You can think of the container as of a placeholder that can hold a fragment at a time; initially it has no fragment and in this case the if(fragment == null) will be met, so you can do a fragment transaction (So, the container layout now is replaced by a fragment view, which is another xml layout assigned to this particular fragment and returned by its onCreateView() callback.onCreateView() is one of fragment's lifecycle callbacks that get called by the system until the fragment is fully visible to the user in the placeholder.
Later on when you want this placeholder to hold another fragment, you can do the transaction again where it repeats the same thing with the new fragment, the placeholder will show up the layout of the new fragment, and the lifecycle methods of this fragment will get called
Should I use FragmentManager? I read in the docs that it's deprecated.
FragmentManger (from android.app package) and its getFragmentManager() are deprecated, and you should use FragmentManager (from androidx.fragment.app package) and getSupportFragmentManager() instead like you already did.
For more info you can have a look to FragmentManager doc and Fragment lifecycle.
It will look into the container with the given ID and give you the Fragment that is currently inside. At no point does findFragmentById initialize a Fragment, it will only look through existing ones, as the name suggests.
You should not use the native FragmentManager on new versions of Android, as it has been deprecated, but the FragmentManager from the AndroidX Fragment library, providing the same functionality.
See also: https://developer.android.com/guide/fragments/fragmentmanager
Related
I want to use Navigation component in my android project, in order to achieve this, I have refactored my app to be fragment-based, but it raises a problem that I don't know how to pass onNewIntent() in my MainActivity to my Navigation Fragments.
So MainActivity is a NavHost, to make it simple, saying that I have a fragment as the start destination, how can I get onNewIntent() inside this fragment from MainActivity?
UPDATE
Also, is there a way to get the reference of the current presented fragment, like the one currently held by NavHost?
On new intent , you can do something like
val navigationController = nav_host_fragment.findNavController()
if (navigationController.currentDestination?.id == R.id.fragmentA) {
(nav_host_fragment.childFragmentManager.fragments[0] as FragmentA).doSomething()
}
Here you can pass any values to the function doSomething()
You can use in MainAcitivy class this:
private Fragment fragment;
fragment = new FragmentTry();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentid, fragment)
.commit();
I have an Activity that handles many Fragments, and, for backstack management, I have a custom stack, where I manage show / hide for Fragments. My code and navigation work perfectly.
Right now, I am implementing the application theme change by a Button in the Configuration Fragment. For this I am using the method Activity.Recreate (); for the change of the theme and, the data of the Configuration Fragment is retained with the same data and the theme of the application changes perfectly, but the BackStack of Fragments disappears, reason why, when pressing the back button, it leaves the application, instead of sending me back to the Fragment or Previous Activity, from where I accessed the Configuration Fragment.
What is the best way to maintain the backstack of my Activity? This is possible?
Important: only when Activity.Recreate(); is called, because if the Activity is destroyed by any other way, I do not want the BackStack back, I want my Activity clean.
Additional:
The orientation setting of my application is in portrait mode.
The launchMode of my Activity is singleTask, it must be so for the type of application I am doing.
From onCreate documentation and this answer.
Add the following logic to your code:
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
// savedInstanceState will be null only when creating the activity for the first time
backstack = new BackStack(); //init your backstack
} else {
// there is a chance that your backstack will be already exists at this point
// if not:
// retrieve the backstack with savedInstanceState.getSerializable("stack")
}
}
And just clear the stack when changing theme, before calling recreate()
// changing theme detected
bacstack.clear();
backstack = null;
recreate();
To save the stack between destruction (onDestroy) and recreation (onCreate) of your activity, Use this method:
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (backstack != null) // the check isn't necessary, you can just put a null in the bundle
outState.putSerializable("stack", backstack);
}
the official guide
for saving UI state
The onSaveInstanceState method helps your activity to Survive
Configuration change and System-initiated process death.
link
My code is as follows:-
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
This is bit weird but it works.
just add android:background="#FFFFFF" attribute to your root layout of the fragment and it will show properly.
been there done that.
Please start by following the best practices, like using Fragment from Support package and using getSupportFragmentManager() instead of getFragmentManager()
Take one step at a time. The above piece of is insufficient for me to provide more information than this.
1) You may be giving wrong handle to the FrameLayout in the view for your activity.
2) You may be mixing Activity, AppCompatActivity, getFragmentManager and getSupportFragmentManager. If your activity is inheriting Activity use getFragmentManager, or if it is using AppCompatActivity use getSupportFragmentManager.
I have been searching and haven't found the best way for nested fragments to communicate with parent fragment. The Android documentation states that:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Suppose I have a Gallery app, there is a ViewPagerFragment (parent) and ImageFragment(child) that is used to show images full screen when they are clicked. I need to pass an ArrayList of Files from my parent fragment to the child fragment. What is a proper way to do it?
1) Create a getter for the ArrayList in parent fragment and use the getParentFragment() in a child fragment to access this method?
2) Implement an interface and go through the Activity, but how will I locate the child fragment from the Activity if it doesn't have an ID?
3) Or use this in parent fragment:
ChildFragment fragment =(ChildFragment)getChildFragmentManager().findFragmentById(R.id.child_fragment_id);
And just use a setter in child fragment.
But again how do I get the ID of the childFragment?
Thanks everyone in advance
If you want to communicate with your child Fragment to your parent Fragment you can use getParentFragment() method from your child fragment.
Example
public class ExampleParentFragment extends Fragment {
public int getCount() {
return 10;
}
}
public class ChildFragment extends Fragment {
void doAction() {
Fragment parentFragment = getParentFragment();
if(parentFragment instanceof ExampleParentFragment) {
int count = ((ExampleParentFragment) parentFragment).getCount();
}
}
}
I understand that the Fragment to Fragment communication through the activity is for sibling fragments which are coordinated by the same activity.
In this case the siblign fragments are coordinated by a parent fragment, so I would say that the communication should be managed by the parent fragment.
If you are working directly with the fragments then you do that using the ChildFragmentManager().
If you are working with a ViewPager you pass the ChildFragmentManager to the ViewPager insted of passing it the FragmentManager.
If you have to pass a List, I think the easiest is for the ChildFragment to request it when it is ready to receive it by calling a method on the ParentFragment. It can allways get its instace with getParentFragment().
I am not sure where is the safest place to call getParentFrament() to get the list but you can try in onCreateView() ,in onActivityCreated(), o in onResume().
To call the method that returns the list, you can cast the parent Fragment to the specific Fragment class you have extended, or else you can define an interface with the method that returns the List and make your extended parent Fragment implement it.
Then in the child Fragment, you refert to the parent by the interface to call the method and get the list.
Data should be passed to fragments using Bundle as arguments. So just pass the Arraylist(serialized or otherwise) to your child fragment when launching it
I have a class that is handling a database in my Android app. When the database changes, I'd like to update the fragment displaying the information from the frogment. My approach has been to give the fragment a tag and then find the fragment with the following code:
FragmentManager manager = getFragmentManager();
Fragment fragment = manager.findFragmentByTag("Schedule");
if (fragment instanceof ScheduleFragment){
ScheduleFragment fr = (ScheduleFragment)fragment;
fr.scheduleUpdated();
}
However, as long as my database class is not an extension of Fragment, the compiler refuses to recognise getFragmentManager(). To me it makes no sense to extend Fragment, as the database class is no fragment, but a simple helper class to manage the database. Is it possible to get a reference to my fragment without extending Fragment? Or is this bad practice and should be done in another way?
Also, is it possible to get a reference to the fragment from a static method?
try using a localBroadcast manager. when database changes laumch a Broadcast intent. registere this in "Schedule" Fragment and you can handle the database changes.
Refer to this link for more about LocalBroadcast Manager
how to use LocalBroadcastManager?
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html