[英]Make app stay in background even if a new activity is launched
I have an application which utilizes a splash screen and a choice screen, as depicted in the following manifest: 我有一个利用启动屏幕和选择屏幕的应用程序,如以下清单所示:
<application
android:name="com.example.CoolApp"
android:label="@string/app_name"
android:icon="@drawable/app_icon_debug"
android:theme="@style/Theme.NoBackground">
<activity
android:name="com.example.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:noHistory="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="coolappscheme"/>
</intent-filter>
</activity>
<activity
android:name="com.example.ChoiceActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"/>
</application>
The splash screen shows for about 2 seconds, and then navigates to the ChoiceActivity
via the following code: 初始屏幕显示约2秒钟,然后通过以下代码导航至ChoiceActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... some stuff for showing the Splash screen ...
Thread mythread = new Thread() {
@Override
public void run() {
try {
.. animation stuff for fading in ...
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {
} finally {
Intent intent = new Intent(Splash.this, ChoiceActivity.class);
startActivity(intent);
}
}
};
Now, clearly there are a number of things wrong with this code (starting with why the author decided to use a separate Thread
instead of an AsyncTask
. However, putting that stuff aside for the moment, if the user performs the following actions: 现在,此代码显然有很多问题(从作者为什么决定使用单独的Thread
而不是AsyncTask
。但是,如果用户执行以下操作,暂时将这些东西放在一边:
Then, when the Thread
completes (ie the finally
block is reached and the Intent
is created), the new ChoiceActivity
is created, but it also brings the application to the foreground. 然后,当Thread
完成时(即,到达了finally
块并创建了Intent
),将创建新的ChoiceActivity
,但同时也会将应用程序置于前台。 I want to disable this behavior. 我想禁用此行为。 That is, I want the activity to be loaded, but the application to remain in the background. 也就是说,我希望加载活动,但应用程序保留在后台。 If that's not possible, then I want the application to delay loading of the new Activity
(//any// new Activity
) until after the Application has been resumed. 如果那不可能,那么我希望应用程序将新Activity
(// any // new Activity
)的加载延迟到恢复应用程序之后。
How can I achieve this? 我该如何实现?
Activities are meant to run in foreground..There is a given lifecycle which is based on how the user interacts with the app. 活动本应在前台运行。存在一个基于用户与应用程序交互方式的生命周期。 Therefore you actually shouldn't be trying to "start your activity in background", because it does not make sense..What you can do though is somehow alter how the activity reacts on events from outside of it.. You can for example create a new boolean field in your activity and set it to false everytime in onPause() and to true in onResume()... 因此,您实际上不应该尝试“在后台启动活动”,因为这没有任何意义。您可以做的是以某种方式改变活动对外部事件的反应。您可以例如创建您的活动中有一个新的布尔字段,每次在onPause()中将其设置为false,在onResume()中将其设置为true ...
You could then check for it when starting the new activity and actually only start it when true. 然后,您可以在启动新活动时检查它,而实际上只有在为true时才启动它。 Otherwise just set the field to true and then in onResume() start the activity when the field would be true.. 否则,只需将字段设置为true,然后在onResume()中启动该字段为true的活动。
Also you should take in account, that background activity could be killed by the system at any time. 另外,您还应考虑到,后台活动可能会随时被系统杀死。 Activity should deallocate all system resources and stop it's work when it goes to background..Only then you can be sure your app won't go into an unpredictable state.. For background tasks you should go with services, which are basically "activities without UI" (I don't believe I've said that) - parts of your app running in the background. 活动应释放所有系统资源,并在其进入后台时停止其工作。只有这样才能确保您的应用程序不会进入不可预测的状态。对于后台任务,您应该使用服务,这些服务基本上是“ UI”(我不敢这么说)-您的应用程序的某些部分在后台运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.