Deep link 导致应用程序的多个实例打开
Deep link is causing multiple instances of the app to open
这个问题已经在类似的帖子中得到解决,但是,我的情况有点不同。我只有一个 activity,还有多个片段。我没有深入link进入特定的片段,我正在启动我的 activity 然后重定向到不同的片段。我 运行 遇到的问题是,当点击 deep link 时,该应用程序的多个实例正在打开,而当阻止该应用程序的多个实例打开时,我丢失了我想要 deep [=] 的数据28=]ing.
我已经用几种方法阻止了多个实例。一种是将 singleTop 添加到我的清单
android:launchMode="singleTop"
这可以防止出现多个实例,但是,我的原始应用程序实例中的静态数据会丢失。另一种方法我也尝试了下面的方法
// finishes activity if its not the root activity
if (!FrameworkUtils.checkIfNull(getIntent().getExtras())) {
if (!isTaskRoot()) {
finish();
}
}
使用此代码,我维护了应用程序的原始实例,但我需要的深层 link 意图数据已经消失,因为应用程序的新实例(我需要)已关闭。
如何解决这种情况,而不必创建额外的 Activity 来启动,然后执行类似
的操作
Intent intent = getIntent();
String intentUrl = intent.getDataString();
Intent newIntent = new Intent(this, MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.putExtra("intentUrl",intentUrl);
newIntent.setAction(Long.toString(System.currentTimeMillis()));
startActivity(newIntent);
finish();
或者更确切地说,如何在用户点击深度 link 后删除应用程序的原始实例并保留应用程序的新实例?提前致谢
请在下面找到 activity 代码,它只有一个实例,您也可以发送数据并进行处理。如果您有任何疑问,请告诉我。
package example.raghavpai;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/**
* Created by RaghavPai on 09-03-2017.
*/
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
processDataFromIntent();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processDataFromIntent();
}
private void processDataFromIntent() {
Intent intent = getIntent();
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
String message = extras.getString("data_key");
}
}
}
public static void startMyActivity(Context context, String data) {
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("data_key", data);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
同样的清单代码
<activity
android:name=".MyActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></activity>
在您的任何活动中使用 public 静态 API startMyActivity。
我找到了一个更适合我的简单解决方案
只需添加这个
if (!isTaskRoot()) {
finish();
}
这个问题已经在类似的帖子中得到解决,但是,我的情况有点不同。我只有一个 activity,还有多个片段。我没有深入link进入特定的片段,我正在启动我的 activity 然后重定向到不同的片段。我 运行 遇到的问题是,当点击 deep link 时,该应用程序的多个实例正在打开,而当阻止该应用程序的多个实例打开时,我丢失了我想要 deep [=] 的数据28=]ing.
我已经用几种方法阻止了多个实例。一种是将 singleTop 添加到我的清单
android:launchMode="singleTop"
这可以防止出现多个实例,但是,我的原始应用程序实例中的静态数据会丢失。另一种方法我也尝试了下面的方法
// finishes activity if its not the root activity
if (!FrameworkUtils.checkIfNull(getIntent().getExtras())) {
if (!isTaskRoot()) {
finish();
}
}
使用此代码,我维护了应用程序的原始实例,但我需要的深层 link 意图数据已经消失,因为应用程序的新实例(我需要)已关闭。
如何解决这种情况,而不必创建额外的 Activity 来启动,然后执行类似
的操作 Intent intent = getIntent();
String intentUrl = intent.getDataString();
Intent newIntent = new Intent(this, MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.putExtra("intentUrl",intentUrl);
newIntent.setAction(Long.toString(System.currentTimeMillis()));
startActivity(newIntent);
finish();
或者更确切地说,如何在用户点击深度 link 后删除应用程序的原始实例并保留应用程序的新实例?提前致谢
请在下面找到 activity 代码,它只有一个实例,您也可以发送数据并进行处理。如果您有任何疑问,请告诉我。
package example.raghavpai;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/**
* Created by RaghavPai on 09-03-2017.
*/
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
processDataFromIntent();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processDataFromIntent();
}
private void processDataFromIntent() {
Intent intent = getIntent();
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
String message = extras.getString("data_key");
}
}
}
public static void startMyActivity(Context context, String data) {
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("data_key", data);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
同样的清单代码
<activity
android:name=".MyActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></activity>
在您的任何活动中使用 public 静态 API startMyActivity。
我找到了一个更适合我的简单解决方案
只需添加这个
if (!isTaskRoot()) {
finish();
}