代码之家  ›  专栏  ›  技术社区  ›  user305774

如何避免每次单击底部导航视图时重新创建新活动

  •  0
  • user305774  · 技术社区  · 6 年前

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

    现在我面临的问题是,每次点击 底部导航视图

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

    <?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;
    }
    

    到目前为止,我已经尝试使用各种意图标志,如标志\活动\重新排序\到\前面,但似乎没有什么工作为我。

    有没有任何可能的解决办法来实现这一点?

    4 回复  |  直到 6 年前
        1
  •  1
  •   Ernest Zamelczyk    6 年前

    您正在通过调用函数从堆栈中删除活动 finish() 你的内心 NavigationItemSelectedListener FLAG_ACTIVITY_REORDER_TO_FRONT

        2
  •  0
  •   Macmist    6 年前

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

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

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

        3
  •  0
  •   Ahmad.ak    6 年前

       class MemoryClass {
       static String value1="";
       }
    

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

           @Override
           public void onPause() {
            super.onPause();
    
            MemoryClass.value1 = editText1.getText().toString();
             }
    
    
        @Override
        public void onResume() {
          super.onResume();
    
          editText1.setText(MemoryClass.value1);
          }
    
        4
  •  0
  •   s.j    6 年前
    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();
                    }