简体   繁体   English

即使启动了新活动,也使应用程序保持在后台

[英]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 。但是,如果用户执行以下操作,暂时将这些东西放在一边:

  1. Launch the application & see the splash screen. 启动应用程序并查看启动屏幕。
  2. Force the application into the background (eg the user receives a phone call, or maybe just hits the 'home' button). 将应用程序强制进入后台(例如,用户接听电话,或者可能只是点击“主页”按钮)。

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.

相关问题 启动应用程序时发生的活动 - Activity that happens when the app is launched 进行新活动时在后台运行应用程序 - Running app in the background when going to a new activity 即使应用程序不在主要焦点中,是否可以在后台应用程序中运行计时器? - Is it possible to make a timer run in the background app even if the app is not in main focus? 在启动应用程序后,按下后​​退按钮会显示相同的活动 - On back button pressed same activity appears when app is launched 如果应用程序在后台运行,如何运行活动? - How to run activity, if app in background? 如何使活动模糊然后随着新活动消失? - How to make an activity blur then disappear with new activity? 使用 Intent 从 Java Activity 启动 Kotlin Activity 时,应用程序崩溃 - App crashes whenever a Kotlin activity is launched from a Java Activity while using Intent 即使不需要ScrollView,如何使Activity可滚动? - how to make an Activity scrollable even if no ScrollView is needed? 当运行新的App Engine应用程序时,Eclipse IDE启动了一个新的App Engine Standard实例 - Eclipse IDE launched a new App Engine Standard instance when a new App Engine app was run onClick进入新活动,但该应用已停止 - onClick to a new activity but the app stopped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM