简体   繁体   English

如何使用Android导航抽屉切换回主活动

[英]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. 您好,我正在开发一个程序,并使用Android Studio为您提供的Navigation Drawer Activity模板制作了一个Navigation抽屉,这是我的主要活动。 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. 我一直在使用片段在应用程序中的页面之间进行切换,但是我将主Activity定义为抽屉中的选项之一,但是似乎无法弄清楚如何使其回到主Activity中。

Here is the main Activity.java File (renamed it for organization) 这是主要的Activity.java文件(为组织重命名)

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 这是content_Main.xml文件

<?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. @ AJok1776我面临同样的问题。

A solution is you create one MainFragment.java same content as your MainActivity (not including navigation drawer) By default load MainFragment page. 一种解决方案是创建一个与MainActivity相同的MainFragment.java内容(不包括导航抽屉)默认情况下,加载MainFragment页面。

MainActivity content is the only Navigation Drawer code. MainActivity内容是唯一的导航抽屉代码。

Then add Home button(or other) in the drawer when user click on home you redirect MainFragment page. 然后,当用户单击主页时,将“主页”按钮(或其他按钮)添加到抽屉中,即可重定向MainFragment页面。

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. 通过将这段代码添加到main.java文件中的if语句中,我设法使其正常工作。

} 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 这是它的视频: 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. 我认为这是事实,这是一项活动,而不是碎片,但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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