Lechat Noir Lechat Noir - 2 months ago 24
Java Question

Item selected automatically

How can I have an item ("R.id.nav_code" for example) automatically selected when I open a page ?

@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_code) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new code())
.commit();
} else if (id == R.id.nav_documentation) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new documentation())
.commit();
} else if (id == R.id.nav_number) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new number())
.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

Answer

If you want to start default fragment everytime your activity start then add this code into your activity onCreate Method

@Override 
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);

FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
code_Fragment code=new code_Fragment(); //Create Object Of your Fragment
transaction.replace(R.id.FragmentLoginRegistration,code);
transaction.commit();
}