Problems in Android Studio with the Fragment that starts by default

0

Good afternoon, I am creating an app that implements a NavigationDrawer, I have selected one of the fragments to be displayed by default ( Home ), it contains an ImageView, everything is fine, but the problem What I have is that when the application is showing another fragment in vertical mode, turning the cell phone and putting it horizontally disappears the fragment that is and returns to the home. I do not know where it is controlled. The following is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_main, new Home()).commit();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    boolean FragmentTransaction = false;
    Fragment fragment = null;

    if (id == R.id.nav_home) {//carga la página principal de la uniagustiniana
        // Handle the camera action
        fragment = new Home();
        FragmentTransaction = true;
    } else if (id == R.id.nav_portal) {
        fragment = new Portal();
        FragmentTransaction = true;
    } else if (id == R.id.nav_biblioteca) {
        fragment = new Biblioteca();
        FragmentTransaction = true;
    } else if (id == R.id.nav_evu) {
        fragment = new Evu();
        FragmentTransaction = true;
    } else if (id == R.id.nav_directorio) {
        fragment = new Directorio();
        FragmentTransaction = true;
    } else if (id == R.id.nav_siga) {
        fragment = new Siga();
        FragmentTransaction = true;
    } else if (id == R.id.nav_moodle) {
        fragment = new Moodle();
        FragmentTransaction = true;
    } else if (id == R.id.nav_face) {
        fragment = new Facebook();
        FragmentTransaction = true;
    } else if (id == R.id.nav_twitter) {
        fragment = new Twitter();
        FragmentTransaction = true;
    } else if (id == R.id.nav_in) {
        fragment = new LinkedIn();
        FragmentTransaction = true;
    } else if (id == R.id.nav_gplus) {
        fragment = new GoogleMas();
        FragmentTransaction = true;
    }

    if(FragmentTransaction){
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_main, fragment)
                .commit();

        item.setChecked(true);
        getSupportActionBar().setTitle(item.getTitle());
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onFragmentInteraction(Uri uri) {

}

}

    
asked by Ivan Alfredo 04.07.2017 в 23:03
source

1 answer

1

That's because when you rotate the device the activity is recreated and its methods are re-executed (natural behavior), that is, the part of putting your fragment "home" is executed again, what you can do is put that part of your code inside an if checking if the onCreate parameter "savedInstanceState" is equal to null, example:

if(savedInstanceState == null) {
   FragmentManager fragmentManager = getSupportFragmentManager();
   fragmentManager.beginTransaction().replace(R.id.content_main, new Home()).commit();
}

With that when the device rotates savedInstanceState will no longer be null so your fragment will no longer be replaced.

    
answered by 05.07.2017 / 02:18
source