[英]How can I show a specific activity when application enters foreground from background?
I have an application with several activities.One of them is Login activity and this activity defined as MAIN in my app in manifest: 我有一个包含多个活动的应用程序,其中一个是Login活动,此活动在清单中我的应用程序中定义为MAIN:
<activity
android:name="com.company.myapp.AuthorizationMainActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The problem when my app enters background (eg I press home button) and then I open the app again - the Login page is showed to me. 当我的应用程序进入后台(例如,我按下主页按钮)然后再次打开该应用程序时出现的问题-向我显示“登录”页面。 How can I show activity which was active for user at the moment application enter background? 当应用程序进入后台时,如何显示对用户有效的活动?
If you don't explicitly want to use it for a special requirement, remove "android:launchMode="singleTask". 如果您不想明确地将其用于特殊要求,请删除“ android:launchMode =“ singleTask”。
A "singleTask" activity allows other activities to be part of its task. “ singleTask”活动允许其他活动成为其任务的一部分。 It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. 它始终是任务的根本,但是其他活动(必要时为“标准”和“ singleTop”活动)也可以启动到该任务中。 (found here at Android Developes) (可在此处找到Android Developes)
Use a dummy activity as your base activity that launches and in that activity check to see if you need to display the login activity or another one. 使用虚拟活动作为启动的基本活动,并在该活动中检查是否需要显示登录活动或另一个活动。 Then start that specific activity you want to go to from the dummy activity. 然后从虚拟活动开始要转到的特定活动。
make sure you set the dummy activity to noHistory
in the manifest so the user cannot navigate back to it 确保在清单中将虚拟活动设置为noHistory
,以便用户无法导航回该活动
if(needsLogIn){
Intent i = new Intent(this,LoginActivity.class);
startActivity(i);
}else{
Intent i = new Intent(this,OtherActivity.class);
startActivity(i);
}
Remove android:launchMode="singleTask" > singletask activity will be the root and when you press home button all the activities above it will be removed. 移除android:launchMode =“ singleTask”> singletask活动将成为根目录,当您按下主屏幕按钮时,其上方的所有活动都将被移除。 when you remove android:launchMode="singleTask" then the default behaviour will take place ie when you press home button and then launch again it will open the activity from where you left. 当您删除android:launchMode =“ singleTask”时,将发生默认行为,即当您按下主屏幕按钮然后再次启动时,它将从您离开的位置打开活动。 have a look at the link http://developer.android.com/guide/components/tasks-and-back-stack.html 看一下链接http://developer.android.com/guide/components/tasks-and-back-stack.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.