1
I have a problem with my studies, when I change Fragement and then I go back to the source Fragment the data is loaded all over again.
And since data comes from the web, it takes time and precious resources, wanted a way that as long as the app was open the already covered screens would not be recharged.
on my onNavigationItemSelected which is on Mainactivity the transition code for fragements is as follows:
if(mFragmentManager.findFragmentByTag("one") != null) {
//if the fragment exists, show it.
mFragmentManager.beginTransaction().show(mFragmentManager.findFragmentByTag("one")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
mFragmentManager.beginTransaction().add(R.id.content, new CategoriaFragment(), "one").commit();
}
but this only prevents the screen from being reloaded when clicked on the menu that opens the current screen.
My main activity is in it that I call my Fragments and control the transition between them.
public class MainActivity extends AppCompatActivity {
private String tituloNavBar;
private TextView mTextMessage;
private CategoriaFragment mCategoriaFragment;
private BuscaFragment mBuscaFragment;
private FragmentManager mFragmentManager;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
tituloNavBar = getResources().getString(R.string.nav_bar_last);
titulo(tituloNavBar);
setFragment(mBuscaFragment,"Busca");
return true;
case R.id.navigation_dashboard:
tituloNavBar = getResources().getString(R.string.nav_bar_start);
titulo(tituloNavBar);
if(mFragmentManager.findFragmentByTag("one") != null) {
//if the fragment exists, show it.
mFragmentManager.beginTransaction().show(mFragmentManager.findFragmentByTag("one")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
mFragmentManager.beginTransaction().add(R.id.content, new CategoriaFragment(), "one").commit();
}
return true;
case R.id.navigation_notifications:
tituloNavBar = getResources().getString(R.string.nav_bar_filter);
titulo(tituloNavBar);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getSupportFragmentManager();
tituloNavBar = getResources().getString(R.string.app_name);
titulo(tituloNavBar);
if(mBuscaFragment == null)
mBuscaFragment = new BuscaFragment();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//Fragmente padrão
setFragment(mBuscaFragment,tituloNavBar);
}
//Substituido pela logica acima
private void setFragment(Fragment fragment, String name){
FragmentManager fm = getSupportFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_left);
ft.replace(R.id.content, fragment);
ft.commit();
}
}
private void titulo(String titulo){
setTitle(titulo);
}
}
is an Activity that contains a menu and can call 3 Fragments, as follows Main activity
- Fragment de busca
- Fragment de categoria
- Fragment de filtro
the result of any of these Fragments would result in a new Activity, would be to show the details, and this Activity would have 3 more Fragments.
How are you implementing your main screen? Why, depending on your application, it will take a check to update the layout. Explain more about your application, please.
– lucas_marciano
@lucas_marciano added an explanation at the end
– Danilo
Danilo, in your Ragment you are loading the data in onResumo or onCreate?
– lucas_marciano
Everything on onCreate, recommends me a better way?
– Danilo
Friend your problem is all in the life cycle of Fragment, I point you to see that video. From what I understand of your problem, you load all your layout in onCreat(), and it is indicated to use onResume to avoid loading Fragment without need, I also saw that you use . add instead of replace, watch this video and one studied on documentation
– lucas_marciano
Thanks, with the video I was able to make my Fragment not be destroyed when rotating the device, but it did not keep the fragement when the fragement change was made, but with the code below I arrived at the solution. Thanks, because I also wanted to keep my Fragment in the rotation too
– Danilo