質問

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:

@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:

  1. Launch the application & see the splash screen.
  2. Force the application into the background (e.g. the user receives a phone call, or maybe just hits the 'home' button).

Then, when the Thread completes (i.e. the finally block is reached and the Intent is created), the new ChoiceActivity is created, but it also brings the application to the foreground. 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.

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()...

You could then check for it when starting the new activity and actually only start it when true. Otherwise just set the field to true and then in onResume() start the activity when the field would be 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top