简体   繁体   中英

How to use Android navigation drawer to switch back to the Main Activity

Hello I am working on a program and have made a navigation drawer with the Navigation Drawer Activity template that Android Studio offers you and that is my main activity. I have been using fragments to switch between pages in my app but I what the main Activity to be one of the options in the drawer, but I can not seem to figure out how to make it go back the the main Activity.

Here is the main Activity.java File (renamed it for organization)

package com.eliteapp.eliteapp_indev;

import android.app.FragmentManager;
import android.os.Bundle;
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.MenuItem;

public class your_cmdrs extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_cmdrs);
        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.addDrawerListener(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 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();


        return super.onOptionsItemSelected(item);
    }

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


        }

        if (id == R.id.GalNet) {
            setTitle("GalNet");
            galnet GalNat = new galnet();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, GalNat).commit();
        } else if (id == R.id.Market) {
            setTitle("Market");
            market Market = new market();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Market).commit();
        } else if (id == R.id.PowerPlay) {
            setTitle("PowerPlay");
            powrplay PowrPlay = new powrplay();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, PowrPlay).commit();


        } else if (id == R.id.ship_fitting) {
            setTitle("Ship Fitting's");
            ship_fitting Ship_Fitting = new ship_fitting();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Ship_Fitting).commit();
        } else if (id == R.id.About) {
            setTitle("About");
            about About = new about();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, About).commit();
        } else if (id == R.id.Settings) {
            setTitle("Settings");
            settings Settings = new settings();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Settings).commit();
        } else if (id == R.id.Change_Log) {
            setTitle("Change Log");
            change_log Chagne_Log = new change_log();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Chagne_Log).commit();
        }

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

and here is the content_Main.xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/Your_CMDRS"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.eliteapp.eliteapp_indev.your_cmdrs"
    tools:showIn="@layout/app_bar_your_cmdrs">

</android.support.constraint.ConstraintLayout>

If anyone could help me with this that would be great.

@AJok1776 I face the same problem.

A solution is you create one MainFragment.java same content as your MainActivity (not including navigation drawer) By default load MainFragment page.

MainActivity content is the only Navigation Drawer code.

Then add Home button(or other) in the drawer when user click on home you redirect MainFragment page.

hope this explanation helps you :)

Edit:

I managed to get it to work by adding this code to the if statement in my main.java file.

} else if (id == R.id.Your_CMDRS) {
            Intent your_cmdrs = new Intent(this, your_cmdrs.class);
            startActivity(your_cmdrs);

Now it works but there is a delay when I switch back to the activity and I can even switch to it even when I am on the main activity

Here is a video of it: https://youtu.be/lQ93OeZDxIY

I think that is do to the fact that it is an activity and not a fragment but I am not sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM