0

I have 5 different activities in my app, i enable the user to navigate between those activities by clicking buttons. The main activity has a RecyclerView in it (used to make a feed). the other activities are used for search, profile, notifications, etc. The user opens the app, the main activity is displayed. The user can scroll through the feed (which is the RecyclerView) in the main activity, now the user decides to go to other activities, search, profile, etc. Now, when the user clicks on the button that navigates to the main activity, I want to open back the main activity and display the recycler view at the same scrolling position and with the same items as was when the user just left it off.

Note: I'm not talking here about pressing the back button or using any goBack functions. I want to enable the user to use the main activity and visit all other activities and after all of that he will be able to come back to the main activity and find the feed exactly as he left it off.

In every activity I have five navigation buttons that allowing the user to go from every activity to every activity: click on profileBtn will open the profile activity, etc.

There is a way in which i can back up the state of the recycler view in the main activity before the user wants to go to other activities and restore it when the user clicks on the homeBtn?

Code:

HomeActivity.java:

public void profileBtn_OnClick(android.view.View view)
{
    Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
    startActivity(intent);
}

ProfileActivity.java, SerachActivity.java, NotificationsActivity.java:

public void homeBtn_OnClick(android.view.View view)
{
    /* HERE I WANT TO OPEN BACK THE HOME ACTIVITY AND DISPLAY THE FEED
       EXACTLY AS IT WAS WHEN THE USER LEFT IT, HOW DO I DO THAT? */

    Intent intent = new Intent(getBaseContext(), HomeActivity.class);
    startActivity(intent);
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Daniel Rotnemer
  • 107
  • 3
  • 12
  • I think you can save the state check this solution here https://stackoverflow.com/questions/27816217/how-to-save-recyclerviews-scroll-position-using-recyclerview-state – Fadman Feb 06 '21 at 22:37

2 Answers2

1

You can use the launchMode="singleTask".

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new one.

You can get more information about launchMode here: https://developer.android.com/guide/topics/manifest/activity-element

android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]

<activity
     android:name=".ui.HomeActivity"
     android:launchMode="singleTask" />
Aman Kumar
  • 244
  • 1
  • 10
  • Use of special launch modes `singleTask` and `singleInstance` are not intended for normal use. These launch modes hace subtle side-effects that are non-obvious and often cause more problems than they solve. See my answer for the correct solution to this problem. – David Wasser Feb 07 '21 at 19:22
1

Please do NOT use the special launch mode singleTask for this purpose. It causes more problems than it solves. These launch modes have complex side-effects that are not obvious and will cause you more problems later.

Your problem is simple to solve using standard Android behaviour. Change your method to look like this:

public void homeBtn_OnClick(android.view.View view)
{
    Intent intent = new Intent(getBaseContext(), HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
}

The flag CLEAR_TOP tells Android to remove all activities from the task that are on top of the target Activity (in this case the target Activity is HomeActivity.

The flag SINGLE_TOP tells Android not to recreate HomeActivity, but to reuse the existing instance of it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    It will be good for others if you mention what kind of problem you faced on using launch mode singleTask. – Aman Kumar Feb 10 '21 at 17:19
  • @AmanKumar Well, I've posted countless answers and comments related to the problems of using the special launch modes. The main problems are confusion/conflict between `launchMode` and `taskAffinity`, also if you use special launch modes and your app has more than 1 `Activity` this confuses users because the will see 2 tasks with the same name and icon in the "recent tasks" list. But there are other subtle problems too. – David Wasser Feb 10 '21 at 18:56