我有这个<intent-filter>
每次按下某个链接时,它都会打开我的应用程序,但问题是它会打开一个new我的应用程序的实例。是否有办法触发 onResume() 并恢复我的应用程序而不丢失其状态或活动堆栈?
这是意图过滤器:
<intent-filter>
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="example.com" />
<data android:pathPattern="/.*" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Update
感谢用户 David Wasser 在下面的回答,我找到了答案:
所以我创建了 EntryActivity ,它在 gmail/inbox 应用程序之上启动:
public class EntryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_activity);
Uri uriParams = getIntent().getData();
Log.e("EntryActivity", uriParams.getHost() );
Log.e("EntryActivity", uriParams.getQueryParameter("uid") + " " + uriParams.getQueryParameter("type") + " " + uriParams.getQueryParameter("token") );
Intent startCategory = new Intent(this, GotEmailActivity.class);
startCategory.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startCategory);
this.finish();
}
}
然后,当我的应用程序在 GotEmailActivity 打开时,我向用户发送电子邮件,其中包含打开应用程序的链接,并且 GotEmailActivity 具有属性android:launchMode="singleTop"
在 AndroidManifest 中,因此仅打开它的 1 个实例:
<!--
Important: notice android:launchMode="singleTop"
which seeks if an instance of this activity is already opened and
resumes already opened instance, if not it opens new instance.
-->
<activity
android:name=".presenters.register.email.GotEmailActivity"
android:label="@string/title_activity_got_email"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
现在发生的情况是 EntryActivity 在 Gmail 应用程序之上打开,但它立即关闭,但首先启动已打开的 GotEmailActivity,因此属性 launchMode Singletop 会阻止此类活动的新实例。