Android:具有多个 Intent 过滤器的 SingleTop 行为

标签 android android-intent android-activity launchmode

我正在努力应对 SingleTop Activity 的以下奇怪行为。

我在该 Activity 中定义了一些 Intent 过滤器:

<activity android:name="com.example.DashboardActivity"
        android:screenOrientation="landscape"
        android:launchMode="singleTop"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="video" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.EDIT" />
            <data android:scheme="survey" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.CALL" />
            <data android:scheme="call" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

此代码应启动 Activity :

webView.setWebViewClient(new WebViewClient() 
    { 
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

                return true;

            }   catch(ActivityNotFoundException e) {

                Log.e(TAG,"Could not load url"+url);
            }

            return super.shouldOverrideUrlLoading(view, url);    

        }

    });

在 DashboardActivity 的 onResume 中,我检查相应的操作:

public void onResume(){
    super.onResume();
    Fragment fragment = null;

    if (Intent.ACTION_VIEW.equals(intent.getAction())) {

        extras.putParcelable("uri", getIntent().getData());
        fragment = new VideoplayerFragment();

    } else {

        fragment = new DashboardFragment();

    }

    addOrReplaceFragment(fragment, extras);
}

但是当我运行这段代码时,我总是得到 android.intent.action.MAIN Action 。如果我删除 singleTop 启动模式,它会启动一个新 Activity ,但会传递正确的 Intent 。我必须使用 Activity 的相同实例,因此 singleTopsingleTasksingleInstance 必须完成这项工作。但我不知道这里出了什么问题。帮助!

最佳答案

Intent.ACTION_VIEW.equals(intent.getAction())

这个 Intent 来自哪里?为了 catch 新的 Intent ,你必须使用 onNewIntent() .

关于Android:具有多个 Intent 过滤器的 SingleTop 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23017475/

相关文章:

android - fragment 自定义 ListView : Getting run time error

android - 具有相同 Intent 的推送通知打开 Activity

android - 带有定向支持的飞溅 Activity

android - 底页横向问题

java - 移动位图不平滑并且不平稳

java - 如何在 Android 上使用 Wobly 序列化器

android - 如何查找 Activity 是否是堆栈中的最后一个 Activity

java - 将微调器数组中的选定值传递给另一个 Android 类

android - Activity之间的动画,其中一个是singleTask

Android:从 'recent applications' 开始应用程序以 Intent 中使用的最后一组额外内容启动它