【发布时间】:2016-07-27 14:00:31
【问题描述】:
在我的应用程序中,我有几个“意图”用于在应用程序中的不同活动之间进行转换。我注意到三星设备上发生了一种奇怪的行为 - 但不是在 Nexus 设备上 - 每当创建新意图时,应用程序都会为这个新活动启动第二个“任务”!当用户进入多任务菜单时,他们可以看到应用程序的多个副本!这不是期望的行为。任何和所有的建议将不胜感激!
清单:
<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。
创建第二个实例的意图:
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;
}
创建第三个实例的意图:
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
}
第三个实例也可以通过启动设置意图来创建:
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**)
}
非常感谢您的帮助!!!
【问题讨论】:
-
您是否尝试仅将 singleInstance 添加到应用程序中?
-
为什么需要单实例启动模式?
-
您拥有的另一个选择是转移到更新的片段生命周期。片段可以在一个活动中全部替换和添加
-
@shaishav 你好,我不需要它用于任何目的 - 我添加它是为了解决我的问题
-
它们用于使活动在自己的任务中运行,这正是您要避免的事情。因此,最好将它们删除。
标签: android android-intent multiple-instances