在我的应用程序中,我有几个“Intent ”,用于在我的应用程序中的不同 Activity 之间进行转换。我注意到一个奇怪的行为发生在三星设备上 - 但不是在 Nexus 设备上 - 每当创建新 Intent 时,应用程序都会为这个新 Activity 启动第二个“任务”!当用户转到多任务菜单时,他们可以看到应用程序的多个副本!这不是期望的行为。任何和所有建议将不胜感激!
list :
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
</activity>
<activity
android:name=".Settings_area"
android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" />
<activity android:name=".Splash"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutPageActivity" />
<activity android:name=".turnOffFromNotification"
android:noHistory="true"></activity>
</application>
我已经尝试删除启动模式并将应用程序启动模式更改为singleTop
和standard
。
创建第二个实例的 Intent:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, splashDisplayLength);
return;
}
创建第三个实例的 Intent :
public void goToAboutPage()
{
Intent goToAboutPage = new Intent(this, aboutPageActivity.class); //create the intent to go to the map screen
startActivity(goToAboutPage); //actually go to the map screen
}
第三个实例可以也通过启动设置 Intent 来创建:
public void changeToSettingsScreen() //changes the screen to the setting screen
{
readyToSendPackets = false;
sendSwitch.setChecked(false);
// textView.setText("NOT sending"); //set the textview to advise users packets are not being sent
Intent goToSettings = new Intent(this, Settings_area.class);
startActivity(goToSettings);
}
我还过度使用了 onNewIntent 方法:
protected void onNewIntent(Intent intent) {
// super.onNewIntent(intent); //REMOVED THIS TO AVOID DOUBLE INSTANTIATION ON TOUCHWIZ IF ANYTHING BREAKS LOOK HERE FIRST
setIntent(intent); //this allows us to recieve the extras bundled with the intent
// System.out.println("Here is the bindle: " + getIntent().getExtras());
if (getIntent().getExtras() != null) //check to see if there are any extras, there wont be on apps first start
{
Bundle extras = getIntent().getExtras(); //get the extras
String methodName = extras.getString("methodName"); //assign the extras to local variables
if(methodName != null && methodName.equals("turn_send_switch_off"))
{
sendSwitch.setChecked(false);
}
//else if(**other actions that may need to be performed can go here**)
}
非常感谢您的帮助!!!
最佳答案
通常,如果您必须强制应用程序的单个实例,您应该避免在每个 Activity 上放置 android:launchMode="singleInstance"
,因为它会尝试为每个 Activity 启动一个实例。
从除应用程序以外的所有内容中删除 launchMode
应该确保只有应用程序在单个实例中运行,虽然@Shaishav 说的是真的,但大多数时候你可以让 android 处理生命周期通过不设置 launchMode
来启动应用程序,除非您确实需要确保一次只有一个实例在运行。
关于android - 每个 Intent 都会在 Android 应用程序中启动一个新任务 - 如何防止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38598362/