首页 > 解决方案 > 每次单击底部导航视图时如何避免重新创建新活动

问题描述

我有一个BottomNavigationView包含 3 个选项。单击时的每个选项都使用 启动一个活动startActivity。然后根据每个活动中的操作,我将在其上附加/替换片段。

现在我面临的问题是,每次单击BottomNavigationView选项时都会创建一个新活动,并且之前打开的活动和附加的片段状态都会丢失。

我想要实现的是,每当单击一个选项时,我只想切换到该活动,如果已经创建并保持其状态。

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="gpacalculator.code.monks.gpacalculator2.BaseActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ToolbarStyle"
/>
<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:id="@+id/result_display_include"
    layout="@layout/display_result_include"
/>
</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/saveFAB"
    android:src="@drawable/ic_save_black_24dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:fabSize="normal"
    app:layout_anchor="@id/result_display_include"
    app:layout_anchorGravity="bottom|right|end"
/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/savedFAB"
    android:src="@drawable/ic_done_black_24dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:fabSize="normal"
    app:layout_anchor="@id/result_display_include"
    app:layout_anchorGravity="bottom|right|end"
    app:backgroundTint="#00cc99"
/>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/frameLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation" />

</android.support.design.widget.CoordinatorLayout>

启动活动的代码片段

@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
    navigationView.postDelayed( () -> {
        int itemId = item.getItemId();

        if (itemId == R.id.navigation_gpa){

            Intent gpaIntent = new Intent(this, GpaActivityContainer.class);
            //gpaIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(gpaIntent);

        }
        else if (itemId == R.id.navigation_cgpa){

            Intent cgpaIntent = new Intent(this, CgpaActivityContainer.class);
            //cgpaIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(cgpaIntent);

        }
        else if (itemId == R.id.navigation_saved){

            Intent savedIntent = new Intent(this, SavedActivityContainer.class);
            //savedIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(savedIntent);

        }
    finish();
    }, 300);
    return true;
}

到目前为止,我已经尝试使用各种意图标志,如 FLAG_ACTIVITY_REORDER_TO_FRONT 但似乎没有什么对我有用。

有没有可能的解决方案如何实现?

那里的所有解决方案都在谈论避免片段重新创建。我很惊讶没有找到活动的解决方案。

标签: androidandroid-activitybottomnavigationview

解决方案


您正在通过finish()调用NavigationItemSelectedListener. 删除此行并将其与标志结合起来,FLAG_ACTIVITY_REORDER_TO_FRONT它应该可以正常工作。


推荐阅读