使用getSupportFragmentManager().beginTransaction().replace()
方法从一个片段(PharmaciesFragment,这是一个Google映射片段)切换到另一个片段(ShoppingCartFragment,有问题的片段)时,它会放大ShoppingCartFragment,然后启动未知活动
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>
活动代码
<?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>
最佳答案
尝试在您的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;
}```