I am adding a BottomNavigationView with three fragments to my app.
Everything works correctly, except one thing.
In the first fragment, there is an EditText view, in the second a ListView and in the third one some texts and images loaded from a JSON hosted in the server.
This is my code:
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_dash:
fragment = new frg_dash();
break;
case R.id.menu_list:
fragment = new frg_list();
break;
case R.id.menu_info:
fragment = new frg_info();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.contenedor_principal, fragment).commit();
return true;
}
});
The problem is that every time I go from one fragment to another using the bottomNavigationView buttons, the Fragment starts all its execution again.
The result I'm looking for is that if the user in the second fragment is, for example, in the middle of the ListView, it goes to the third fragment and returns again, the ListView continues where it was.
Or if you press the button of the third Fragment in the bottomNavigationView, do not load the data again from the server.
I guess the problem is that when you click on a bottomNavigationView button, the fragment is created again:
... switch (id) {
case R.id.menu_dash:
fragment = new frg_dash();
break; ...
But it's just a guess. I suppose it can be controlled with the onCreate, onActivityCreated and onCreateView methods, but again, they are just my assumptions.
I've tried it with the hide () and show () parameters of the fragments, but without success ... or I'm not applying it well
I greatly appreciate the help in advance.
EDIT
This is my example currently with all the parts related to the answer:
public void replaceFragment(Fragment fragment, #Nullable Bundle bundle, boolean popBackStack, boolean findInStack) {
Log.v("2134", "Dentro");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
String tag = fragment.getClass().getName();
Log.v("2134", "tag:" + tag);
Fragment parentFragment;
if (findInStack && fm.findFragmentByTag(tag) != null) {
parentFragment = fm.findFragmentByTag(tag);
} else {
parentFragment = fragment;
}
// if user passes the #bundle in not null, then can be added to the fragment
if (bundle != null) {
parentFragment.setArguments(bundle);
} else {
parentFragment.setArguments(null);
}
// this is for the very first fragment not to be added into the back stack.
if (popBackStack) {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
ft.addToBackStack(parentFragment.getClass().getName() + "");
}
ft.replace(R.id.contenedor_principal, parentFragment, tag);
ft.commit();
fm.executePendingTransactions();
}
...
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.menu_panel:
fragment = new frg_panel();
break;
case R.id.menu_promos:
fragment = new frg_promociones();
break;
case R.id.menu_catalogo:
fragment = new frg_catalogo();
break;
}
replaceFragment(fragment, null, true, true);
return true;
}
});
Use this code to open your fragment. Your fragment will not create every time. It will get same fragment from stack if exist.
/**
* replace or add fragment to the container
*
* #param fragment pass android.support.v4.app.Fragment
* #param bundle pass your extra bundle if any
* #param popBackStack if true it will clear back stack
* #param findInStack if true it will load old fragment if found
*/
public void replaceFragment(Fragment fragment, #Nullable Bundle bundle, boolean popBackStack, boolean findInStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
String tag = fragment.getClass().getName();
Fragment parentFragment;
if (findInStack && fm.findFragmentByTag(tag) != null) {
parentFragment = fm.findFragmentByTag(tag);
} else {
parentFragment = fragment;
}
// if user passes the #bundle in not null, then can be added to the fragment
if (bundle != null)
parentFragment.setArguments(bundle);
else parentFragment.setArguments(null);
// this is for the very first fragment not to be added into the back stack.
if (popBackStack) {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
ft.addToBackStack(parentFragment.getClass().getName() + "");
}
ft.replace(R.id.contenedor_principal, parentFragment, tag);
ft.commit();
fm.executePendingTransactions();
}
use it like
Update :
If your fragment is home or dashboard fragment then
Fragment f = new YourFragment();
replaceFragment(f, null, true, true);
Otherwise
Fragment f = new YourFragment();
replaceFragment(f, null, false, true);
Important This code is not replacement of saving all states or variables in fragment. This code will useful because it will not create fragment instance again.
For saving all states and variables in fragment for future use see this answer
Related
I'm using a NavigationDrawer and one Activity where I'm replacing Fragments within the FrameLayout. Navigating through NavigationDrawer and replacing Fragments inside of the Activity is straightforward. I'm not sure how to handle when I replace a Fragment from another Fragment. Example:
I show a Fragment which is a basic List, and when the user clicks on one of the items, I show a new Fragment. Now, from this new Fragment, I may choose to show another Fragment, which results in a deeper Fragment hierarchy. I know that I can navigate through with
.addToBackStack() and in the case of Back button, call
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
from the Activity, but as deeper the Fragment hierarchy gets, the more complicated and dirty it becomes. Is there a good article, or architectural approach to handle a case like this one?
In your scenario you should use Nested Fragments.
You can embed fragments inside fragments.
To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment(). And then your parent backstack and child backstack will be separated.
This is how to do it right. The other question is maybe you should consider using intent and new activity instead of 10 nested fragments? Here is an article about child fragments.link
Stack similar question
Make Fragments tags in your Main Activity:
//----------------Fragment TAGS-----------------------\\
private final String FRAGMENT_BOOK_RIDE = "Frag Book Ride";
private final String FRAGMENT_RIDE_HISTORY = "Frag Ride History";
private final String FRAGMENT_PAYMENT = "Frag Payment";
private final String FRAGMENT_PROFILES_SETTINGS = "Frag Profile & Settings";
private final String FRAGMENT_HELP = "Frag Help";
//-----------------------------------------------------\\
Navigation item selection:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here. \\
int id = item.getItemId();
switch (id) {
case R.id.nav_book_ride:
removeAllFragments();
getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
break;
case R.id.nav_ride_history:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new RideHistoryFragment(getApplicationContext())
, FRAGMENT_RIDE_HISTORY)
.commit();
break;
case R.id.nav_payment:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new PaymentFragment(getApplicationContext())
, FRAGMENT_PAYMENT)
.commit();
break;
case R.id.nav_settings:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new ProfileSettingFragment(getApplicationContext())
, FRAGMENT_PROFILES_SETTINGS)
.commit();
break;
case R.id.nav_help:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new HelpFragment(getApplicationContext())
, FRAGMENT_HELP)
.commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
Handling backKey press:
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
backKeyMethods();
}
}
/**
* Custom back key method for managing fragment back key behavior
*/
private void backKeyMethods() {
RideHistoryFragment rideHistoryFragment = (RideHistoryFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_RIDE_HISTORY);
PaymentFragment paymentFragment = (PaymentFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_PAYMENT);
HelpFragment helpFragment = (HelpFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_HELP);
ProfileSettingFragment userProfileFragment = (ProfileSettingFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_PROFILES_SETTINGS);
if (rideHistoryFragment != null && rideHistoryFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (paymentFragment != null && paymentFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (userProfileFragment != null && userProfileFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (helpFragment != null && helpFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (getFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
/**
* Remove all fragments in Fragment BackStack
*/
private void removeAllFragments() {
FragmentManager fm = getSupportFragmentManager();
for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
}
Use addToBackStack(null) whenever you want to go deep, so when you will press backKey it will pop immediate fragment.
Hope it helps.
I'm currently utilizing the Navigation Drawer for my Android APP. In my first fragment, I've a fragment that loads data using Facebook's Graph API. Thus, when my App is first loaded, it first goes to the first fragment.
Then, I use the Navigation Drawer to click on another Fragment and view it.
And then finally, I reuse the Navigation Drawer to proceed back to the first Fragment and view it.
My issue that I'm facing is, how do I proceed to utilize the Fragment that has been created once instead of re-creating it whenever the Navigation Drawer Item is selected. My code for the switching of the fragments are as shown below.
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new SelectionFragment();
break;
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new SplashFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I noticed that there is indeed a "new" instance of the Fragment every time whenever the option is selected. How do I go about implementing the logic of creating the Fragment instance ONCE and reusing it, so that I do not have to continuously load the Fragment over and over again.
To anyone who encounters the same issue with me,I've managed to find a solution.
In the container frame,I've to define specific fragment views that I'll be utilizing as shown below.
In each Fragment view,I've to "link" it with the actual Fragment itself as shown below via the "android:name" attribute.Do take note of the the path to the Fragment,example for in my case it's com.example.confesssionsrp.SelectionFragment.
<fragment
android:id="#+id/selectionFragment"
android:name="com.example.confessionsrp.SelectionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In the the MainActivity(or the Activity where we're viewing the fragments),create variables for each of the Fragments as shown below.
Following that,in the Oncreate of the MainActivity(or your specific Activity),initialize the various fragments as shown below.
Proceed onto creating a new Method called "ShowFragment" as shown below.
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
if (Session.getActiveSession().isClosed()) {
mDrawerLayout
.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
From then on in the switching of the fragments,manually call upon the "ShowFragment" method with the selected Fragment as shown below.
Doing all of this overall,will not reset the Fragment each time we view it,and therefore is the solution to the answer.Thank you for anyone who has helped me so far :)!
I am using the following code:
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
if(position==0){// selection of tabs content
fragmentManager
.beginTransaction()
.replace(R.id.container,
SimulatorFragment.newInstance(position + 1)).commit();
}else if(position==1){
fragmentManager
.beginTransaction()
.replace(R.id.container,
HudFragment.newInstance(position + 1)).commit();
}else if(position==2){
// Display the fragment as the main content.
fragmentManager
.beginTransaction()
.replace(R.id.container,
SettingsBasicFragment.newInstance(position +1)).commit();
}else{
}
}
You can replace by a new instance the first time and store the fragment, if it is not null, then replace by the stored fragment.
The activity must implement NavigationDrawerFragment.NavigationDrawerCallbacks
The fragment constructor and newInstance methods look like this:
public final class HudFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
* #param simulation
*/
public static HudFragment newInstance(int sectionNumber) {
HudFragment fragment = new HudFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public HudFragment() {
}
To switch fragments by code I use this method inside the NavigationDrawerFragment:
/**
* Select a different section
* #param position
*/
public void select(int position){
selectItem(position);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
The second option is to start with the example of navigationDrawer that Android SDK offers. I selected that template of activity when creating the project and almost all the code of my previous answer is produced automatically.
If you want to keep the fragments after device rotation or similars it is a different thing, you need then to retain the fragments. If not, you just need to save the new instance of the fragment in a variable and check if it is null to create a new one or use the old one.
In case someone want's a different approach to this: you could find the fragment on the stack:
// check if this fragment is on the backstack to avoid creating a new one
Fragment foundFragment = fragmentManager.findFragmentByTag("unique_fragment_tag");
if (foundFragment != null) {
fragmentManager.popBackStackImmediate("unique_fragment_tag", 0);
return;
}
None of the other questions I have read on stackoverflow have been able to help with my problem. As far as I can tell, I am doing everything correctly.
I have a master/detail flow with fragments.
Upon creation of the main activity, the master fragment is loaded with the following code:
Fragment frag;
frag = new MainListFragment();//<-- **the master fragment**
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, frag);
Log.d("My Debug Bitches", "stack:" + fm.getBackStackEntryCount());
transaction.commit();
The master fragment has a ListView; clicking on a list item brings up the details fragment like so:
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
SubListFragment frag = new SubListFragment();//<-- **the detail fragment**
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
fm.executePendingTransactions();
Log.d("My Debug Bitches", "stack:" + fm.getBackStackEntryCount());
}
Now, according to LogCat, the BackStackEntryCount changes from 0 to 1 after I navigate from master fragment to detail fragment:
So why is it that, when I click the back button while in the details fragment, that the app closes instead of returning to the master fragment??????????
You have to add the popBackStack() call to the onBackPressed() method of the activity.
Ex:
#Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStack();
} else {
super.onBackPressed();
}
}
#Bobbake4's answer is awesome, but there is one little problem.
Let's say I have three fragments A, B and C.
A is the main Fragment (the fragment that shows when I launch my app), B and C are fragments I can navigate to from the navigation drawer or from A.
Now, when I use the back button from B or C, I go back to the previous fragment (A) alright, but the title of the previous fragment (fragment B or C) now shows in the actionBar title of Fragment A. I have to press the back button again to "truly" complete the back navigation (to display the view and correct title for the fragment and returning to)
This is how I solved this problem. Declare these variables.
public static boolean IS_FRAG_A_SHOWN = false;
public static boolean IS_FRAG_B_SHOWN = false;
public static boolean IS_FRAG_C_SHOWN = false;
In the MainActivity of my app where am handling navigation drawer methods, I have a method displayView(position) which handles switching of my fragments.
private void displayView(int position) {
IS_FRAG_A_SHOWN = false;
IS_FRAG_B_SHOWN = false;
IS_FRAG_C_SHOWN = false;
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
IS_FRAG_A_SHOWN = true;
break;
case 1:
fragment = new FragmentB();
IS_FRAG_B_SHOWN = true;
break;
case 2:
fragment = new FragmentC();
IS_FRAG_C_SHOWN = true;
break;
default:
break;
}
finally, in my onBackPressed method, I do this:
public void onBackPressed() {
if(fragmentManager.getBackStackEntryCount() != 0) {
fragmentManager.popBackStack();
if (IS_FRAG_A_SHOWN) { //If we are in fragment A when we press the back button, finish is called to exit
finish();
} else {
displayView(0); //else, switch to fragment A
}
} else {
super.onBackPressed();
}
}
I'm working on an android application, that uses a navigation drawer to switch between two fragments. However, each time I switch, the fragment is completely recreated.
Here is the code from my main activity.
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
android.support.v4.app.Fragment fragment;
String tag;
android.support.v4.app.FragmentManager; fragmentManager = getSupportFragmentManager();
switch(position) {
case 0:
if(fragmentManager.findFragmentByTag("one") != null) {
fragment = fragmentManager.findFragmentByTag("one");
} else {
fragment = new OneFragment();
}
tag = "one";
break;
case 1:
if(fragmentManager.findFragmentByTag("two") != null) {
fragment = fragmentManager.findFragmentByTag("two");
} else {
fragment = new TwoFragment();
}
tag = "two";
break;
}
fragment.setRetainInstance(true);
fragmentManager.beginTransaction().replace(R.id.container, fragment, tag).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mNavTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
I've set up some debug logging, and every time selectItem is called, one fragment is destroyed, while the other is created.
Is there any way to prevent the fragments from being recreated, and just reuse them instead?
After #meredrica pointed out that replace() destroys the fragments, I went back through the FragmentManager documentation. This is the solution I've come up with, that seems to be working.
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
android.support.v4.app.FragmentManager; fragmentManager = getSupportFragmentManager();
switch(position) {
case 0:
if(fragmentManager.findFragmentByTag("one") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("one")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.container, new OneFragment(), "one").commit();
}
if(fragmentManager.findFragmentByTag("two") != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("two")).commit();
}
break;
case 1:
if(fragmentManager.findFragmentByTag("two") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("two")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.container, new TwoFragment(), "two").commit();
}
if(fragmentManager.findFragmentByTag("one") != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("one")).commit();
}
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mNavTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
I also added this bit, but I'm not sure if it's necessary or not.
#Override
public void onDestroy() {
super.onDestroy();
FragmentManager fragmentManager = getSupportFragmentManager();
if(fragmentManager.findFragmentByTag("one") != null){
fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("one")).commit();
}
if(fragmentManager.findFragmentByTag("two") != null){
fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("two")).commit();
}
}
Use the attach/detach method with tags:
Detach will destroy the view hirachy but keeps the state, like if on the backstack; this will let the "not-visible" fragment have a smaller memory footprint. But mind you that you need to correctly implement the fragment lifecycle (which you should do in the first place)
Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.
The first time you add the fragment
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.add(android.R.id.content, new MyFragment(),MyFragment.class.getSimpleName());
t.commit();
then you detach it
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.detach(MyFragment.class.getSimpleName());
t.commit();
and attach it again if switched back, state will be kept
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.attach(getSupportFragmentManager().findFragmentByTag(MyFragment.class.getSimpleName()));
t.commit();
But you always have to check if the fragment was added yet, if not then add it, else just attach it:
if (getSupportFragmentManager().findFragmentByTag(MyFragment.class.getSimpleName()) == null) {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.add(android.R.id.content, new MyFragment(), MyFragment.class.getSimpleName());
t.commit();
} else {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.attach(getSupportFragmentManager().findFragmentByTag(MyFragment.class.getSimpleName()));
t.commit();
}
The replace method destroys your fragments. One workaround is to set them to Visibility.GONE, another (less easy) method is to hold them in a variable. If you do that, make sure you don't leak memory left and right.
I did this before like this:
if (mPrevFrag != fragment) {
// Change
FragmentTransaction ft = fragmentManager.beginTransaction();
if (mPrevFrag != null){
ft.hide(mPrevFrag);
}
ft.show(fragment);
ft.commit();
mPrevFrag = fragment;
}
(you will need to track your pervious fragment in this solution)
I guess you can not directly manipulate the lifecycle mechanisms of your Fragments. The very fact that you can findFragmentByTag is not very bad. It means that the Fragment object is not recreated fully, if it is already commited. The existing Fragment just passes all the lifecycle steps each Fragment has - that means that only UI is "recreated".
It is a very convenient and useful memory management strategy - and appropriate, in most cases. Fragment which is gone, has the resources which have to be utilized in order to de-allocate memory.
If you just cease using this strategy, the memory usage of your application could increase badly.
Nonetheless, there are retained fragments, which lifecycle is a bit different and do not correspond to the Activity they are attached to. Typically, they are used to retain some things you want to save, for example, to manage configuration changes
However, the fragment [re]creation strategy depends on the context - that is, what you would like to solve, and what are the trade-offs that you are willing to accept.
Just find the current fragment calling getFragmentById("id of your container") and then hide it and show needed fragment.
private void openFragment(Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment existingFragment = fragmentManager.findFragmentByTag(tag);
if (existingFragment != null) {
Fragment currentFragment = fragmentManager.findFragmentById(R.id.container);
fragmentTransaction.hide(currentFragment);
fragmentTransaction.show(existingFragment);
}
else {
fragmentTransaction.add(R.id.container, fragment, tag);
}
fragmentTransaction.commit();
}
Same idea as Tester101 but this is what I ended up using.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment oldFragment = fragmentManager.findFragmentByTag( "" + m_lastDrawerSelectPosition );
if ( oldFragment != null )
fragmentTransaction.hide( oldFragment );
Fragment newFragment = fragmentManager.findFragmentByTag( "" + position );
if ( newFragment == null )
{
newFragment = getFragment( position );
fragmentTransaction.add( R.id.home_content_frame, newFragment, "" + position );
}
fragmentTransaction.show( newFragment );
fragmentTransaction.commit();
Hide easily in kotlin using extensions:
fun FragmentManager.present(newFragment: Fragment, lastFragment: Fragment? = null, containerId: Int) {
if (lastFragment == newFragment) return
val transaction = beginTransaction()
if (lastFragment != null && findFragmentByTag(lastFragment.getTagg()) != null) {
transaction.hide(lastFragment)
}
val existingFragment = findFragmentByTag(newFragment.getTagg())
if (existingFragment != null) {
transaction.show(existingFragment).commit()
} else {
transaction.add(containerId, newFragment, newFragment.getTagg()).commit()
}
}
fun Fragment.getTagg(): String = this::class.java.simpleName
Usage
supportFragmentManager.present(fragment, lastFragment, R.id.fragmentPlaceHolder)
lastFragment = fragment
Here's what I'm using for a simple 2 fragment case in Kotlin:
private val advancedHome = HomeAdvancedFragment()
private val basicHome = HomeBasicFragment()
override fun onCreate(savedInstanceState: Bundle?) {
...
// Attach both fragments and hide one so we can swap out easily later
supportFragmentManager.commit {
setReorderingAllowed(true)
add(R.id.fragment_container_view, basicHome)
add(R.id.fragment_container_view, advancedHome)
hide(basicHome)
}
binding.displayModeToggle.onStateChanged {
when (it) {
0 -> swapFragments(advancedHome, basicHome)
1 -> swapFragments(basicHome, advancedHome)
}
}
...
}
With this FragmentActivity extension:
fun FragmentActivity.swapFragments(show: Fragment, hide: Fragment) {
supportFragmentManager.commit {
show(show)
hide(hide)
}
}
How about playing with the Visible attribute?
this is a little late response.
if you're using view pager for fragments, set the off screen page limit of the fragment to the number of fragments created.
mViewPager.setOffscreenPageLimit(3); // number of fragments here is 3
Ok, I've stated that fragments are removed without reason. It seems to me, like this, but sureley there is a reason I don't know about. Please help me find it.
I got the FragmentActivity which contains TabHost, with 5 tabs. TabHost has a onTabChangedListener like this (nothing complicated here, just a case with copied code):
new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
LiveTabFragment fragment = null;
int viewHolder = -1;
switch (tabHost.getCurrentTab()) {
case TAB_ABOUT:
fragment = (LiveAboutFragment) getSupportFragmentManager().findFragmentByTag(LiveAboutFragment.class.getSimpleName());
if (fragment == null) fragment = new LiveAboutFragment();
viewHolder = R.id.live_tab_about;
break;
case TAB_EVENTS:
fragment = (LiveEventsFragment) getSupportFragmentManager().findFragmentByTag(LiveEventsFragment.class.getSimpleName());
if (fragment == null) fragment = new LiveEventsFragment();
viewHolder = R.id.live_tab_events;
break;
case TAB_PLAYERS:
fragment = (LivePlayersFragment) getSupportFragmentManager().findFragmentByTag(LivePlayersFragment.class.getSimpleName());
if (fragment == null) fragment = new LivePlayersFragment();
viewHolder = R.id.live_tab_players;
break;
case TAB_LEGEND:
fragment = (LiveLegendFragment) getSupportFragmentManager().findFragmentByTag(LiveLegendFragment.class.getSimpleName());
if (fragment == null) fragment = new LiveLegendFragment();
viewHolder = R.id.live_tab_legend;
break;
case TAB_CHAT:
fragment = (LiveChatFragment) getSupportFragmentManager().findFragmentByTag(LiveChatFragment.class.getSimpleName());
if (fragment == null) fragment = new LiveChatFragment();
viewHolder = R.id.live_tab_chat;
break;
}
if (fragment != null) injectInnerFragment(fragment, viewHolder, false);
}
}
Here is an injectInnerFragment method:
public void injectInnerFragment(AaFragment fragment, int viewHolderId, boolean addToBackStack) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (fragment.isAdded()) {
transaction.replace(viewHolderId, fragment, fragment.getClass().getSimpleName());
if (addToBackStack) transaction.addToBackStack(null);
transaction.commit();
} else {
transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
if (addToBackStack) transaction.addToBackStack(null);
transaction.commit();
}
}
Now the problem:
When I click on tab for thr first time fragment is beeing created (onCreate is called) which is normal. In most cases second clicking on already created tab fragment doesn't call fragment'sonCreate which is what I wanted. Thats why I'm trying to find fragment if FragmentManager first.
There are two cases it doesn't work, and previously created fragment is created again, which is not efficient for me. The cases are:
if I click on any tab, then on TAB_ABOUT, then clicking again on any tab causes it's recreating ("any tab fragment" is not found in FragmentManager)
if I click on any tab, then on TAB_CHAT, then clicking again on any tab causes it's recreating ("any tab fragment" is not found in FragmentManager)
What kind of sorcery is this? Is it some automatic memory freeing, dependant on fragment "weight"? Maybe I should store all data I don't want reload each time fragment is created on Activity instead?
Look at your following code :
transaction.replace(viewHolderId, fragment, fragment.getClass().getSimpleName());
try with :
transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
Solved this by modyfing inject method:
public void injectInnerFragment(AaFragment fragment, int viewHolderId, boolean addToBackStack) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (fragment.isAdded()) {
transaction.attach(fragment);
transaction.commit();
} else {
transaction.add(viewHolderId, fragment, fragment.getClass().getSimpleName());
if (addToBackStack) transaction.addToBackStack(null);
transaction.commit();
}
}