我有这样一个问题<intent-filter>
:每次按下某些链接时,它都会打开我的应用程序,但问题是它会打开我的应用程序的新实例。无论如何,是否有触发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>
感谢用户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中打开我的应用程序时,我会向用户发送带有打开应用程序链接的电子邮件,并且GotEmailActivityandroid: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阻止了此类活动的新实例。
Activity
响应时,应该创建另一个用作应用程序入口点的应用程序<intent-filter>
。像这样:
您需要的只是一个简单的活动,什么也不做。这是一个例子:
public class EntryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check to see if this Activity is the root activity
if (isTaskRoot()) {
// This Activity is the only Activity, so
// the app wasn't running. So start the app from the
// beginning (redirect to MainActivity)
Intent mainIntent = getIntent(); // Copy the Intent used to launch me
// Launch the real root Activity (launch Intent)
mainIntent.setClass(this, MainActivity.class);
// I'm done now, so finish()
startActivity(mainIntent);
finish();
} else {
// App was already running, so just finish, which will drop the user
// in to the activity that was at the top of the task stack
finish();
}
}
}
将您的<intent-filter>
活动而不是“发布者”活动。确保清单中此活动的任务亲和力与应用程序中其他活动的任务亲和力相同(默认情况下,如果您未显式设置android:taskAffinity,则为)。
当<intent-filter>
被触发,如果你的应用程序正在运行,那么EntryActivity
将开始在上面的应用程序的任务,最上面的活动和任务将被带到前台。当EntryActivity
完成后,它会简单地返回用户最上面的活动在您的应用程序(即:无论用户在哪里离开它,当它走进背景)。
如果您的应用未运行,则EntryActivity
识别出该错误并从头开始启动您的应用,并将Intent
触发的ACTION和DATA传递给它<intent-filter>
。
应该管用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句