0

I have a BottomNavigationView containing 3 options. Each option on clicking starts an activity using startActivity. Then based on the operations in each activity, i will just attach/replace fragments on it.

Now the problem i am facing is, each time on clicking BottomNavigationView option a new activity is created and also previously opened activity and the attached fragment state is lost.

What i want to achieve is whenever an option is clicked i just want to switch to that activity if already created maintaining its state.

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>

Code snippet where the activity is launched

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

So far i have tried using various intent flags like FLAG_ACTIVITY_REORDER_TO_FRONT but nothing seems to be working for me.

Is there any possible solution how this can be achieved?

All the solution out there talks about avoiding fragment re-creating. I am surprised that no where i could find a solution for activity.

user305774
  • 123
  • 15

4 Answers4

1

You're removing the activity from the stack by invoking function finish() inside of your NavigationItemSelectedListener. Remove this line and combine it with the flag FLAG_ACTIVITY_REORDER_TO_FRONT and it should be working correctly.

Ernest Zamelczyk
  • 2,562
  • 2
  • 18
  • 32
0

have you tried to change launchmode of your activity, in your manifest? You can use singleTop, which will detect if an activity already exists, and if that's the case an intent will be sent to that instance instead of recreating a new instance.

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

You also have other choices such as SingleInstance and singleTask that ensure one and only one instance of the activity are created, although those are not recommended to use in most cases.

You can read more about the uses of launchmodes here: documentation

Macmist
  • 713
  • 3
  • 11
  • But the documentation says old instance will be used only if the activity is in the top of the stack. But in my case always it may not be on the top of the stack. – user305774 Sep 30 '18 at 20:06
0

You have many options to save activity or fragment state, for me i found the best way by creating a memory class contains static variables like this:

   class MemoryClass {
   static String value1="";
   }

and in Activity use onPause() to save state and onResume() to retrieve state like this:

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

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


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

      editText1.setText(MemoryClass.value1);
      }
Ahmad.ak
  • 93
  • 6
0
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();
                }
s.j
  • 621
  • 6
  • 16