如何防止activity在访问其他活动后返回时重新加载自己?
How to prevent activity from reloading itself when coming back after visiting other activities?
我的应用程序中有 5 个不同的活动,我允许用户通过单击按钮在这些活动之间导航。主 activity 中有一个 RecyclerView
(用于制作提要)。其他活动用于搜索、个人资料、通知等。用户打开应用程序,显示主要 activity。用户可以滚动浏览主要 activity 中的提要(即 RecyclerView
),现在用户决定转到其他活动、搜索、个人资料等。现在,当用户单击导航到主 activity 的按钮,我想打开主 activity 并在相同的滚动位置显示回收器视图,并使用与用户刚刚关闭时相同的项目。
注意: 我在这里不是在谈论按下 back
按钮或使用任何 goBack
功能。我想让用户能够使用主 activity 并访问所有其他活动,在所有这些之后他将能够回到主 activity 并完全按照他离开时的样子找到提要.
在每个 activity 中,我有五个导航按钮,允许用户从每个 activity 转到每个 activity:单击 profileBtn
将打开配置文件 activity,等等
有一种方法可以让我在用户想去其他活动之前备份主要 activity 中回收站视图的状态,并在用户单击 [=18] 时恢复它=]?
代码:
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);
}
您可以使用 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.
您可以在此处获取有关启动模式的更多信息:https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
请不要为此目的使用特殊启动模式singleTask
。它造成的问题多于解决的问题。这些启动模式具有不明显的复杂副作用,以后会给您带来更多问题。
使用标准 Android 行为很容易解决您的问题。将您的方法更改为如下所示:
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);
}
标志 CLEAR_TOP
告诉 Android 从任务中删除位于目标 Activity
之上的所有活动(在这种情况下,目标 Activity
是 HomeActivity
.
标志 SINGLE_TOP
告诉 Android 不要重新创建 HomeActivity
,而是重用它的现有实例。
我的应用程序中有 5 个不同的活动,我允许用户通过单击按钮在这些活动之间导航。主 activity 中有一个 RecyclerView
(用于制作提要)。其他活动用于搜索、个人资料、通知等。用户打开应用程序,显示主要 activity。用户可以滚动浏览主要 activity 中的提要(即 RecyclerView
),现在用户决定转到其他活动、搜索、个人资料等。现在,当用户单击导航到主 activity 的按钮,我想打开主 activity 并在相同的滚动位置显示回收器视图,并使用与用户刚刚关闭时相同的项目。
注意: 我在这里不是在谈论按下 back
按钮或使用任何 goBack
功能。我想让用户能够使用主 activity 并访问所有其他活动,在所有这些之后他将能够回到主 activity 并完全按照他离开时的样子找到提要.
在每个 activity 中,我有五个导航按钮,允许用户从每个 activity 转到每个 activity:单击 profileBtn
将打开配置文件 activity,等等
有一种方法可以让我在用户想去其他活动之前备份主要 activity 中回收站视图的状态,并在用户单击 [=18] 时恢复它=]?
代码:
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);
}
您可以使用 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.
您可以在此处获取有关启动模式的更多信息:https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
请不要为此目的使用特殊启动模式singleTask
。它造成的问题多于解决的问题。这些启动模式具有不明显的复杂副作用,以后会给您带来更多问题。
使用标准 Android 行为很容易解决您的问题。将您的方法更改为如下所示:
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);
}
标志 CLEAR_TOP
告诉 Android 从任务中删除位于目标 Activity
之上的所有活动(在这种情况下,目标 Activity
是 HomeActivity
.
标志 SINGLE_TOP
告诉 Android 不要重新创建 HomeActivity
,而是重用它的现有实例。