Hy,
I am developing an application with Fragments. Here is my (simplified) code.
I thought that in "onNavigationItemSelected", the replaced fragment will free the memory that it took. But I see that if I switch fragments many times, the free memory will always decrease.
So, I would like to know why, and if I can prevent this from happening
Thanks in advance for your answers.
I am developing an application with Fragments. Here is my (simplified) code.
Java:
public class MainActivity extends AppCompatActivity{
private FragmentManager fm = null;
private Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
FragmentManager fm = getSupportFragmentManager();
fragment = Fragment1.newInstance();
fm.beginTransaction().replace(R.id.content_to_replace,fragment).commit();
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.fragment_1) {
fragment = Fragment1.newInstance();
idFragment = 0;
} else if (id == R.id.fragment_2) {
fragment = Fragment2.newInstance();
idFragment = 1;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
fm.beginTransaction().replace(R.id.content_to_replace,fragment).commit();
return true;
}
}
I thought that in "onNavigationItemSelected", the replaced fragment will free the memory that it took. But I see that if I switch fragments many times, the free memory will always decrease.
So, I would like to know why, and if I can prevent this from happening
Thanks in advance for your answers.
