Hello! I am a beginner and I can not figure it out please help. There is a NavigationView and several pages fragment. Problem: for example, I’m in the category snippet and I flip the device and the data is reset and goes to the main screen. How to solve this problem?


OnCreate () code of the main activation:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); //убрал ненужные коды navigationView.setNavigationItemSelectedListener(this); transaction.beginTransaction().replace(R.id.container, fragment_main).commit(); } 

and here is the onNavigationItemSelected () method:

  switch (item.getItemId()) { case R.id.nav_main : transaction.beginTransaction().replace(R.id.container,fragment_main).commit(); break; case R.id.nav_report_category: transaction.beginTransaction().replace(R.id.container,fragment_category).commit(); break; case R.id.nav_history: transaction.beginTransaction().replace(R.id.container,fragment_history).commit(); break; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } 

    1 answer 1

    My magic ball says, you add a fragment to the activation onCreate and do not check if there is another fragment already in the activation.

    Those. If you don’t have a fragment, if you don’t have it, add the main fragment.

    Also, if you have a fragment, God forbid, it is indicated in the markup - remove it from there and add it programmatically.

    • I also do this with the transaction.replace () method. - Temirlan Abdullaev
    • but that's not the problem. and when changing the screen orientation - Temirlan Abdullaev
    • I think this is related to OnCreate () Navigation Drawer 'a. but I don’t know how to solve it - Temirlan Abdullayev
    • turning the screen, the current fragment is not saved - Temirlan Abdullaev
    • You somewhere displays the first fragment. Where you can only guess the code is not visible - YuriySPb