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

我在该活动中定义了一些意图过滤器:

<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>

此代码应该启动该活动:

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 行动。如果我删除 singleTop 启动模式,它启动一个新的活动,但传递正确的意图。我必须使用该活动的相同实例,所以 singleTop, singleTask 或者 singleInstance 必须完成这项工作。但我不知道这里出了什么问题。帮助!

有帮助吗?

解决方案

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

这个意图从何而来?为了捕捉新的意图,你必须使用onNewIntent().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top