我有一个利用启动屏幕和选择屏幕的应用程序,如以下清单所示:
<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>
初始屏幕显示约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);
}
}
};
现在,显然,此代码存在很多问题(从作者为什么决定使用单独的Thread
而不是。的原因开始AsyncTask
。但是,如果用户执行以下操作,请暂时将这些内容放在一边:
然后,当Thread
完成操作(即finally
到达该块并Intent
创建)时,将ChoiceActivity
创建新的,但也将应用程序置于前台。我想禁用此行为。也就是说,我希望活动被加载,但是应用程序保留在后台。如果那不可能,那么我希望应用程序将新的Activity
(// any // new Activity
)的加载延迟到恢复应用程序之后。
我怎样才能做到这一点?
活动本应在前台运行。.有一个给定的生命周期,该生命周期基于用户与应用程序的交互方式。因此,您实际上不应该尝试“在后台启动活动”,因为这没有任何意义。.您可以做的是以某种方式更改活动对外部事件的反应。您的活动中有一个新的布尔字段,每次在onPause()中将其设置为false,在onResume()中将其设置为true ...
然后,您可以在启动新活动时检查它,而实际上只有在为true时才启动它。否则,只需将字段设置为true,然后在onResume()中将字段设置为true时启动活动。
另外,您还应考虑到,后台活动可能会随时被系统杀死。活动应释放所有系统资源,并在其进入后台时停止其工作。只有这样,您才能确保您的应用不会进入不可预测的状态。对于后台任务,您应该使用服务,这些服务基本上是“没有活动的活动”。 UI”(我不敢这么说)-您的应用程序的某些部分在后台运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句