简体   繁体   English

当应用程序从后台进入前台时,如何显示特定活动?

[英]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.

相关问题 如何杀死android中的其他前台活动? - How can I kill other foreground activity in android? 当鼠标进入组件控件时,如何阻止 SWT 复合控件触发 MouseExit 事件? - How can I stop an SWT composite control from firing a MouseExit event when the mouse enters a component control? 如何使用Single Activity或Java类检测应用程序运行在前台还是后台 - How to detect application runs foreground or background using Single Activity or Java class 我如何更改 JDateChooser 背景和前景色 - How can i change JDateChooser background and foreground color 如何根据文本所在的背景更改文本的前景色? - How can I change the foreground color of a text based on the background it is on? 如何从后台服务更新Android活动中的信息 - How can I update information in an Android Activity from a background Service 如何在前台显示 windows 后台进程 - How to show windows background process in the foreground 如何在对话框的前台显示我自己的Toast? - How can I show my own Toast in the foreground of a Dialog? 当用户在Spring启动应用程序中输入无效URL时,如何显示自定义404页面? - How to show custom 404 page when user enters invalid URL in spring boot application? 如何从适配器获取数据并在Android中的Activity中显示 - how can i get data from adapter and show in Activity in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM