片段膨胀时,导航抽屉启动未知活动

时间:2019-07-03 02:44:17

标签: java android android-fragments navigation-drawer

使用getSupportFragmentManager().beginTransaction().replace()方法从一个片段(PharmaciesFragment,这是一个Google映射片段)切换到另一个片段(ShoppingCartFragment,有问题的片段)时,它会放大ShoppingCartFragment,然后启动未知活动< / p>

MenuActivity的代码

public class MenuActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
LinearLayout navHeaderLayout;
View headerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    initializeWidgetsAndListeners(savedInstanceState);
}

private void initializeWidgetsAndListeners(Bundle savedInstanceState) {
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    drawerLayout = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new PharmaciesFragment()).commit();
        navigationView.setCheckedItem(R.id.map);
    }
    headerView = navigationView.getHeaderView(0);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.map:
            if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new PharmaciesFragment()).commit();
            }
            break;
        case R.id.shopping_cart:
            if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ShoppingCartFragment()).commit();
                finish();
            }
        case R.id.profile:
            intent = new Intent(this, ProfileActivity.class);
            startActivity(intent);
            finish();
            break;
        case R.id.settings:
            intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            finish();
            break;

    }

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

}

ShoppingCartFragment的代码

package com.example.pharmaship;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ShoppingCartFragment extends Fragment {
    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable             
ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_shopping, null, false);
    return view;
    }
}

fragment_shopping.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_centerInParent="true"
        android:text="MESSAGE"/>

</RelativeLayout>

activity_menu的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:context=".MenuActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

            tools:targetApi="lollipop" >
            <ImageView
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:src="@drawable/logo"
                android:layout_gravity="start"
                android:layout_marginLeft="20dp"
                android:id="@+id/toolbar_logo"
                android:layout_marginStart="85dp" />
        </androidx.appcompat.widget.Toolbar>

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

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

尝试在finish()侦听器中删除onNavigationItemSelected方法

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.map:
            if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new PharmaciesFragment()).commit();
            }
            break;
        case R.id.shopping_cart:
            if (!navigationView.getMenu().getItem(item.getOrder()).isChecked()) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ShoppingCartFragment()).commit();
                finish(); // <-- REMOVE THIS LINE
            }
            break; // And add the break instruction
        case R.id.profile:
            intent = new Intent(this, ProfileActivity.class);
            startActivity(intent);
            finish();
            break;
        case R.id.settings:
            intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            finish();
            break;

    }

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