[英]Manage Multiple Activity, Resume Activity with Sub Activity instead Starting New
[英]Resume the Top Activity instead of starting the Launcher Activity
我在我的应用程序中有两个活动,一个是启动器,另一个是从第一个启动的显式调用。
这里我的问题是,当我通过从第二个活动按下主页键返回主屏幕并启动应用程序时,即使第二个活动已经在后台,第一个活动也会启动。
编写第一个Activity是为了下载应用程序工作所需的资产,一旦下载资产,它就会触发第二个活动并调用自我完成。
以下是我的应用程序清单。
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<! Download the Required Assets if not found on SD Card -->
<activity android:name=".ContentDownload"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true">
<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.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ActualAppActivity"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<supports-screens android:smallScreens="false" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true"/>
有人可以指导我如何使第二个活动直接获得控制,而不是在调用启动器并且它在后台时再次通过第一个活动。
下面是我的onResult回拨方法。
public void onResult(String assetPath, int result)
{
if(result == RESULT_OK)
{
startActivity(new Intent(this, ActualAppActivity.class));
activity.destroyDownloadActvity();
finish();
}
else
{
finish();
java.lang.System.exit(0);
}
activity.destroyDownloadActvity();
activity = null;
}
尝试在Manifest中指定为Launcher Activity的活动的onCreate方法中使用以下代码,即来自问题中发布的原始代码的ContentDownload活动:
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
这将在显示Launcher活动之前通过检测到已经有任务正在运行来完成它,并且您的应用应该恢复到最后一个可见的活动。
请参阅有关Android Manifest launchModes的Android文档中的此页面: http : //developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode
你已经用launchMode="singleTask"
定义了你的活动。 这是你问题的根源。 删除它。
我用下面的代码LAUNCHER
我的应用程序的活动,以防止beeing再次启动应用程序时,它的背景和图标还活着被窃听
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isTaskRoot()) {
finish();
return;
}
// Rest of your onCreate stuff goes here
}
这只是完成了LAUNCHER
活动并恢复了最后一次使用的活动。
不要调用finish()。 您需要将标志FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK传递给Intent。
Intent intent = new Intent(this, ActualAppActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
没有看到你的代码,我想你想要这样的回答:
你需要设置intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
你还需要finish()
你的发射器活动。
你可以做的是在持久存储中存储一个字符串,用于确定资产是否已经加载。
然后,在主活动中,您可以检查资产是否已加载,然后启动所需的活动。
if(assets_already_downloaded)
second_activity();
else
download_asset_activity();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.