Menu screens are above the app’s home screen

Asked

Viewed 776 times

1

I was implementing the standard Navigation Drawer and when I emulated the apk was the screens of the options above the initial screen of the apk, this way:

erro

Mainactivity.java file.

 @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.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_second_activity) {
        return true;
    }

    return super.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 faith:

    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;
    }
}

Because it’s superimposing on another Intent and not changing?

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

erro1 erro2

content_main.xml file

 <?xml version="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>
  • from what I’m seeing you put a listview along with a @id/content_frame what’s wrong! since you will use this file(screen) content_main.xml to make the transition from Fragments? correct?

  • Check out this post, where I teach how to transition between screens http://answall.com/questions/119551/menu-de-3-pontos-em-todas-activities-com-o-padr%C3%A3o-navigation-Drawer/119561#119561

  • this one of yours listview should disappear, or you should put these items in the default menu Navigation Drawer - in activity_main_drawer.xml, or create a menu of items in another Fragment and call this Fragment through the @+id/content_frame and make the transition of the screens!

2 answers

1

Good afternoon,

Try to add

.addToBackStack() 

in the transition of Fragment.

I had a similar problem, you see here

I hope I’ve 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;
}
  • Marco still not understood how to add . addToBackStack() correctly in my code.

  • after the replace you give in the fragmentManager for example : fragmentManager.beginTransaction(). replace(R.id.content_frame, new Timeline()). addToBackStack("Example"). commit();

  • Okay, sorry for the questions, I’m still new in android studio... in place of this example I call what?

  • I edited the answer.. just replace in your code and test

  • I did as you mentioned above, but it remains the same. A screen above the other.

  • 1

    "Because this overlapping the other Intent and not changing?" from what I saw, you do not change only the one replace in Fragment..

  • Is the project early? Particularly I use this library to use Drawer link, is stable and flexible...

  • I’m just trying to take advantage of the Drawer method that android studio already implements when starting a project. I think it got confused, reformulating, I’m in the main Internet and I want to go to Facebook where the menu options are, but I see your screens and if I click will be answered by the Main screen.

  • The Fragments will overlap or is only the main screen "below" and above is only the last Fragment?

  • Marco is only the last Ragment on top of the start screen

  • Mark your face? Since I can’t chat yet

  • Hi is that link

Show 7 more comments

0


To call a new Activity():

Create a new Activity by the name of MainActivity2 and insert this code below into your method onNavigationItemSelected of his MainActivity:

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

To call a new Fragment(Fragment):

Create a new Fragment by the name of novoFragment and insert this code below into your method onNavigationItemSelected of his MainActivity:

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

** Important Notice **

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

To learn more, see the example in this post: 3-point menu in all Activities with standard 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;
}
  • Insert this code below into your onNavigationItemSelected method of your Mainactivity??? of the new Activity or main? Create a new Fragment under the name of newFragment and insert this code below into your onNavigationItemSelected method of your Mainactivity: in which Activity?

  • @Carlosdiego in his Mainactivity.java (main activity).

  • How do you call in the chat?

  • It was yes, there’s no time.

  • 1

    Otima dica Paulo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.