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.
Related
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
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();
The following is some Android java code:
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = new ThisClassExtendsFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
I am confused by the call to beginTransaction(). It seems that this is an abstract method, and that it isn't implemented anywhere. How then can it return a FragmentTransaction object?
It seems that this is an abstract method, and that it isn't implemented anywhere
The implementation is in an internal part of the Android SDK, that you will not be able to see from the JavaDocs.
In this case, it is implemented by FragmentManagerImpl, a nested class inside the FragmentManager class.
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
How do you access the ActionBar from within a Fragment (android.support.v4.app.Fragment). I am using the Android Support Library v4 and v7. I'm not using the Sherlock library's.
The activity hosting the fragment is an ActionBarActivity.
I have read the Android help section Fragment which led me too getActivity() but there is no getSupportActionBar() method.
ActionBar actionBar = getActivity().getSupportActionBar()
I have read the Android help section Fragment which led me too getActivity() but there is no getSupportActionBar() method.
You will need to cast the result of getActivity() to be an ActionBarActivity, on which you will find getSupportActionBar().
If you are using AppCompatActivity then do it like this.
((AppCompatActivity )getActivity()).getSupportActionBar();
Also it is advised to shift to Toolbar with AppCompatActivity instead of action bar(app bar). For more details refer this http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html