Android 恢复 Top Activity 而不是启动 Launcher Activity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11721619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resume the Top Activity instead of starting the Launcher Activity
提问by Sudhaker
I have two activities in My application, one being launcher and the other is launched as a explicit call from the first.
我的应用程序中有两个活动,一个是启动器,另一个是作为第一个活动的显式调用启动的。
Here My problem is when i go back to home screen by pressing home key from second activity and launch the application, again the first activity gets initiated even though the second activity is already in the background.
这里我的问题是当我通过从第二个活动按主页键返回主屏幕并启动应用程序时,即使第二个活动已经在后台,第一个活动也会再次启动。
The first Activity is written to download the required assets for the application to work, once the assets are downloaded it triggers the second activity and calls finish for self.
第一个 Activity 用于下载应用程序运行所需的资产,一旦下载了资产,它就会触发第二个 Activity 并为 self 调用完成。
Below is my manifest of the application.
以下是我的应用程序清单。
<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"/>
Can Somebody please guide me on how to make the second activity gain the control directly instead of going through the first again if launcher is called and it is in the background.
有人可以指导我如何让第二个活动直接获得控制权,而不是在调用启动器并且它在后台时再次通过第一个活动。
Below is my onResult Call back method.
下面是我的 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;
}
采纳答案by Jadent
Try using the following code in the onCreate method of the activity that is specified as the Launcher Activity in the Manifest, i.e. the ContentDownload activity from the original code posted in the question:
尝试在清单中指定为启动器活动的活动的 onCreate 方法中使用以下代码,即问题中发布的原始代码中的 ContentDownload 活动:
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
This will finish your Launcher Activity before it is displayed by detecting that there is already a task running, and your app should instead resume to the last visible Activity.
这将通过检测到已经有一个任务在运行而在显示之前完成您的 Launcher Activity,并且您的应用程序应该恢复到最后一个可见的 Activity。
See this page in the Android documentation regarding Android Manifest launchModes: http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode
请参阅有关 Android 清单启动模式的 Android 文档中的此页面:http: //developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode
回答by David Wasser
You've got both your activities defined with launchMode="singleTask"
. This is the root of your problem. Remove that.
你已经用launchMode="singleTask"
. 这是你问题的根源。去掉那个。
回答by Saenic
I use the following code in the LAUNCHER
Activities of my apps to prevent the app from beeing started again when its still alive in the background and the icon is tapped
我在LAUNCHER
我的应用程序的活动中使用以下代码来防止应用程序在后台仍然活着并且图标被点击时再次启动
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isTaskRoot()) {
finish();
return;
}
// Rest of your onCreate stuff goes here
}
This just finishes the LAUNCHER
Activity and resumes the last used one.
这只是完成了LAUNCHER
Activity 并恢复了上次使用的Activity。
回答by Uriel Frankel
Do not call finish(). You need to pass to the Intent the flag FLAG_ACTIVITY_CLEAR_TASKand FLAG_ACTIVITY_NEW_TASK.
不要调用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);
回答by themanatuf
Without seeing your code, I think you want something like this answer:
没有看到你的代码,我想你想要这样的答案:
Android finish Activity and start another one
You need to set intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
and you'll also need to finish()
your launcher activity as well.
您需要设置intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
并且还需要finish()
启动器活动。
回答by Tanuj Wadhwa
what you can do is store a string in the persistent storage which determines whether the assets have been loaded before or not.
您可以做的是在持久存储中存储一个字符串,以确定之前是否已加载资产。
Then, in the main activity, you may check if the assets have been loaded or not and then start the required activity.
然后,在主活动中,您可以检查资产是否已加载,然后启动所需的活动。
if(assets_already_downloaded)
second_activity();
else
download_asset_activity();