Menu screens are above the application start screen

1

I was implementing the Navigation Drawer standard and when I emulated the apk the options screens were over the apk home screen, like this:

MainActivity.javaFile

@OverridepublicvoidonBackPressed(){DrawerLayoutdrawer=(DrawerLayout)findViewById(R.id.drawer_layout);if(drawer.isDrawerOpen(GravityCompat.START)){drawer.closeDrawer(GravityCompat.START);}else{super.onBackPressed();}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.menu_main,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();//noinspectionSimplifiableIfStatementif(id==R.id.action_second_activity){returntrue;}returnsuper.onOptionsItemSelected(item);}@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.servic) {

    } else if (id == R.id.nav_camera) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Cronograma())
                .commit();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Localizacao())
                .commit();
    } else if (id == R.id.nav_share) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Sobre_aplicativo())
                .commit();
    } else if (id == R.id.nav_manage) {
        finish();

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

My fragment:

    public class Localizacao  extends Fragment {
    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.local, container, false);
        return view;
    }
}

Why is it overriding the other intent and not changing?

I noticed that in the code it shows me these messages:

Content_main.xmlfile

<?xmlversion="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

Activity_main_drawer file:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/servic"
            android:icon="@drawable/servico"
            android:title="Serviços" />
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/pref"
            android:title="o local" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/local"
            android:title="Localização" />

    </group>

    <item android:title="Informações">
        <menu>

            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/dev"
                android:title="Sobre o aplicativo" />
            <item
                android:id="@+id/nav_manage"
                android:icon="@drawable/exit"
                android:title="Sair" />
        </menu>
    </item>

</menu>
    
asked by anonymous 05.06.2016 / 20:45

2 answers

0

To call up a new Activity:

Create a new Activity with the name of MainActivity2 and enter this code below in your onNavigationItemSelected method of your MainActivity :

Intent intent = new Intent(this, MainActivity2.class); 
startActivity(intent);

To call a new Fragment:

Create a new Fragment with the name of novoFragment and enter this code below in your onNavigationItemSelected method of your MainActivity :

novoFragment fragment = new novoFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();

** Important Notice **

  

In order for the call of this fragment to be successfully completed (work),   it is necessary to create a new fragment ( novoFragment ) and make use of another fragment and that it is empty (in this case I used content_main.xml )   with FrameLayout with id ex: @+id/fragment_container to   that the transition between the screens occurs.

     

To learn more, see the example in this post:    3-point menu in all Activities with Navigation Drawer

In your MainActivity - Solution in Practice:

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

    if (id == R.id.nav_camera) {
        // abrindo um novo fragment
     novoFragment fragment = new novoFragment();
    android.support.v4.app.FragmentTransaction fragmentTrasaction =
    getSupportFragmentManager().beginTransaction();
    fragmentTrasaction.replace(R.id.fragment_container, fragment);
    fragmentTrasaction.commit();
    } else if (id == R.id.nav_gallery)  {
    // abrindo um nova activity
    Intent intent = new Intent(this, MainActivity2.class); 
    startActivity(intent);    
    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
    
05.06.2016 / 23:12
0

Good afternoon,

Try adding

.addToBackStack() 

in the transition of the fragment.

I had a similar problem, you can see here

I hope I have helped.

[Edit]

Try this

public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.servic) {

    } else if (id == R.id.nav_camera) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Cronograma())
                .addToBackStack("teste")  //adiciona esta linha
                .commit();
    } else if (id == R.id.nav_gallery) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Localizacao())
                .addToBackStack(null) //acho que também funciona com null
                .commit();
    } else if (id == R.id.nav_share) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Sobre_aplicativo())
                .addToBackStack("teste2")  //
                .commit();
    } else if (id == R.id.nav_manage) {
        finish();

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
    
05.06.2016 / 20:52