从导航抽屉登录到android

时间:2018-02-04 13:47:43

标签: android

我在导航抽屉活动中遇到问题,我创建了具有登录活动和导航抽屉活动的android项目,但是当我想从导航抽屉注销到登录活动时。我没有来,我已经尝试了一切。以下是我的代码。请帮我解决这个问题。

activity_home.xml

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home"
    app:menu="@menu/activity_home_drawer" />

NavigationDrawer.xml

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_vehicle_enq"
        android:title="Vehicle Enquiry" />
    <item
        android:id="@+id/nav_my_vehicle"
        android:title="My Vehicles" />
    <item
        android:id="@+id/nav_my_rides"
        android:title="My Rides" />
    <item
        android:id="@+id/nav_licence_details"
        android:title="Licence Details" />
    <item
        android:id="@+id/nav_setting"
        android:title="Setting" />
    <item
        android:id="@+id/nav_help"
        android:title="Help" />
    **<item
        android:id="@+id/nav_logout"
        android:title="Logout"
        android:onClick="out"/>**

</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Share" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Send" />

    </menu>
</item>

Navigation.java

public class HomeActivity扩展了AppCompatActivity         实现NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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 onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, 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_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

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


        // Handle the camera action
    } else if (id == R.id.nav_my_vehicle) {

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


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

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

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

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

        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

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

}

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomeActivity"
        android:label="@string/title_activity_home"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".RegisterActivity" />
    <activity android:name=".vehicle_enquiryActivity" />


</application>

2 个答案:

答案 0 :(得分:0)

添加此代码

else if (id == R.id.nav_logout) {
    Intent intent = new Intent(getApplicationContext(), YourLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

答案 1 :(得分:0)

希望我来不及帮助......

好的,首先你必须在你拥有导航抽屉的Activity的类的根目录中声明一个onNavigationItemSelected方法。然后你需要编写一个switch语句,它为Drawer中的每个项添加功能,如图所示下面:

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();

        switch (id) {
            case R.id.item_home:
                Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_userProfile:
                Toast.makeText(this, "User Profile", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_recycleSites_Maps:
                Toast.makeText(this, "Waste and Recycle Sites", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_settings:
                Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
                break;
                case R.id.item_logOut:
                    logOut();
                    return true;
            }

drawerLayout.closeDrawer(GravityCompat.START);
        return true;
}

logOut();行指的是我在下面创建的方法(也在类根中),如下所示:

private void logOut() {
        mAuth.signOut();
        sendToLogin();
    }

最后,sendToLogin();是对完成此操作所需的最后一种方法的引用......

private void sendToLogin() {

        Intent loginIntent = new Intent(HomeActivity.this, LoginActivity.class);
        startActivity(loginIntent);
        finish();

    }

创建两个额外的类而不是简单地在switch语句中编写Intent的原因是由于良好的实践,如果您的应用程序中有多个导航抽屉,则会变得更加实用,在我的情况下是的。

希望这会有所帮助,如果您还有其他问题,请随时提出!