深层链接导致应用程序的多个实例打开
此问题已在类似帖子中解决,但是,我的情况有点不同。我只有一个活动,和多个片段。我不是深入链接到特定的片段,我启动我的一个活动,然后重定向到不同的片段。我遇到的问题是,在单击深层链接时以及在防止应用程序的多个实例打开时,应用程序的多个实例处于打开状态,因此我失去了深层链接意图中的数据。深层链接导致应用程序的多个实例打开
我已经阻止了多个实例的几种方法。一个是被添加singleTop到我的清单
android:launchMode="singleTop"
这样可以防止多个实例,但是,从我原来的应用程序实例中的静态数据丢失。另一种方式我也曾尝试以下方式以及
// finishes activity if its not the root activity
if (!FrameworkUtils.checkIfNull(getIntent().getExtras())) {
if (!isTaskRoot()) {
finish();
}
}
有了这个代码,我保持应用程序的原始实例,但目的数据我从深层链接需要走了,因为应用程序的新实例(我需要)被关闭。
我怎样才能解决这个问题,而不需要创建额外的活动推出,然后做一些像
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();
或者说,我怎么可以删除该应用程序的原始实例,并保持的新实例用户点击深层链接后的应用程序?在此先感谢
请在下面找到只有一个实例的活动代码,您也可以发送您的数据并进行处理。如果您有任何疑问,请告诉我。对于同一
<activity
android:name=".MyActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></activity>
使用从您的任何活动的公共静态API startMyActivity
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);
}
}
清单代码。
是的,我知道如何拥有1个应用程序实例。这就是你所描述的。但是这并不能完全回答我的问题,即如何在单击深度链接之前保留原始静态对象。 – portfoliobuilder
为了您明白我的意思,请保留相同的代码,但要创建一个递增的计数器。按下CTA触发电子邮件的深层链接(例如),将该计数器增加为1.现在最小化该应用程序,当您按下电子邮件深层链接并将其重定向回到您的应用程序时,您会注意到该计数器重置为0.是的,你有一个应用程序实例,但是你保留了新实例并销毁了原始实例。这是问题。 – portfoliobuilder
对不起,我错了。你是对的。有用! – portfoliobuilder
你是什么意思'应用程序的新实例(我需要)被关闭'?这不是'singleTop'的作用。你在哪里重写[onNewIntent](https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))? – ianhanniballake
我的意思是应用程序的原始实例被关闭。抱歉错字。第二种方式是关闭应用程序的新实例。但是有两种方法都有问题,因为应用程序的新实例没有来自我的静态对象的数据。相反,如果应用程序的新实例被关闭,则原始实例不具有来自推送通知的数据。 – portfoliobuilder
您的程序随时可以随时移除(随着用户从您的应用程序切换到其他应用程序,它会一直发生在低端设备上)。无论如何你都需要能够从中恢复。 – ianhanniballake