When starting Navigation Drawer enter with a fragment directly

4

it's just that, I'm creating an app which has a login (main activity) and a record, when I pass this process, I enter a Navigation Drawer activity I have the fragments but it is blank, when I step on it the options of that menu are loaded the fragments of correct form, my problem is that I want that it opens already with the fragment of beginning and that does not load in white. if you need me to add something I will do it immediately (it's worth noting that I'm totally new in both this android and on the web but I hope to learn to also help others: D)

package company.viral.organizadorjec;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import company.viral.organizadorjec.Fragment.AjustesF;
import company.viral.organizadorjec.Fragment.CalendarioF;
import company.viral.organizadorjec.Fragment.InicioF;
import company.viral.organizadorjec.Fragment.PerfilF;
import company.viral.organizadorjec.Fragment.ProfesoresF;

public class Menucentral extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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();

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

@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.menucentral, 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();

    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_camera) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new ProfesoresF()).commit();
    } else if (id == R.id.nav_slideshow) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new CalendarioF()).commit();
    } else if (id == R.id.nav_manage) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new AjustesF()).commit();
    } else if (id == R.id.nav_share) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new PerfilF()).commit();
    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

    
asked by ERny JOsé HIdalgo COrrea 25.11.2016 в 19:59
source

2 answers

3

At the start of your MainActivity, you can add the fragment you decide within onCreate() , for example, assuming your fragment at startup is called FragmentInicial :

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

/*********Agrega fragmento **************/

   FragmentManager fragmentManager = getSupportFragmentManager(); 
  fragmentManager.beginTransaction().replace(R.id.contenedor,new FragmentInicial()).commit();

/*********Agrega fragmento **************/

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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();

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

the transaction of the other fragments would be made when selecting an item from the menu, which you have already implemented:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_camera) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new ProfesoresF()).commit();
    } else if (id == R.id.nav_slideshow) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new CalendarioF()).commit();
    } else if (id == R.id.nav_manage) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new AjustesF()).commit();
    } else if (id == R.id.nav_share) {
        fragmentManager.beginTransaction().replace(R.id.contenedor,new PerfilF()).commit();
    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
    
answered by 25.11.2016 / 20:52
source
0

You have to implement this method, obviously making the necessary changes

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

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});

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();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setFragment(0)//aqui llamas al metodo que te ddejo abajo y en el metodo, cambias listalibrogragment() por el nombre de tu Fragment, y el "R.id.contentPrincipal" es el id tu content_main_drawer

}

public void setFragment(int position) {

    switch (position) {
        case 0:
            FragmentManager fragmentManager;
            FragmentTransaction fragmentTransaction;
            fragmentManager = getFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.content_principal, new ListaLibroFragment());
            fragmentTransaction.addToBackStack(null).commit();
            break;

    }
}

and call it in your onCreate () method of the navigationDrawer

    
answered by 25.11.2016 в 20:09