如何避免每次单击底部导航视图时都重新创建一个新的 activity

How to avoid recreating a new activity every time when bottom navigation view is clicked

我有一个包含 3 个选项的 BottomNavigationView。单击每个选项都会使用 startActivity 启动 activity。然后根据每个activity中的操作,我将attach/replace个片段放在上面。

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

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

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>

启动 activity 的代码片段

@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,但似乎没有任何效果。

是否有任何可能的解决方案如何实现?

所有解决方案都在谈论避免重新创建片段。我很惊讶找不到 activity 的解决方案。

您是否尝试过在清单中更改 activity 的启动模式?您可以使用 singleTop,它将检测 activity 是否已经存在,如果是这种情况,将向该实例发送一个意图,而不是重新创建一个新实例。

<activity
    android:name="your_activity_name"
    android:launchMode="singleTop"
    // rest of your activity declaration
    />

您还有其他选择,例如 SingleInstance 和 singleTask,它们确保只创建一个 activity 实例,尽管在大多数情况下不建议使用这些实例。

您可以在此处阅读有关启动模式用途的更多信息:documentation

你有很多选项来保存 activity 或片段状态,对我来说我发现最好的方法是创建一个内存 class 包含如下静态变量:

   class MemoryClass {
   static String value1="";
   }

并在 Activity 中使用 onPause() 保存状态并使用 onResume() 检索状态,如下所示:

       @Override
       public void onPause() {
        super.onPause();

        MemoryClass.value1 = editText1.getText().toString();
         }


    @Override
    public void onResume() {
      super.onResume();

      editText1.setText(MemoryClass.value1);
      }

您正在通过调用 NavigationItemSelectedListener 中的函数 finish() 从堆栈中删除 activity。删除此行并将其与标志 FLAG_ACTIVITY_REORDER_TO_FRONT 组合,它应该可以正常工作。

to avoid recreation of activtiy just use FLAG_ACTIVITY_CLEAR_TOP.

public void onClick(DialogInterface dialog, int id) {
                    Intent intent=new Intent(DriverAssignedTask.this,MapsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }