Chrome自定义标签打开为Chrome中的新标签而不是应用内部的原因?

时间:2017-12-24 18:49:25

标签: java android android-customtabs

如何在我的应用中打开自定义标签,而无需在后台打开Goog​​le Chrome ...

这是我的代码

String a="http://www.gpamravati.ac.in/gpamravati";
        CustomTabsIntent.Builder builder=new CustomTabsIntent.Builder();
        CustomTabsIntent custom=builder.build();
        custom.intent.setPackage("com.android.chrome");
        builder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
        custom.launchUrl(this, Uri.parse(a));

when I click on website menu item it invokes the chrome custom tab

It opens like this

2 个答案:

答案 0 :(得分:3)

最后通过添加

解决了我的问题
android:launch mode="singleTask"

只需将此代码添加到活动

下的清单中即可

以下是输出 output

答案 1 :(得分:0)

简而言之, 你应该打开一个片段OnNavigationItemSelectedListener并在打开的片段中使用webview并打开Url。

片段

xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

         <WebView
         android:id="@+id/main_webview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
</FrameLayout>
片段

java 代码

    public class UrlFragment extends Fragment  {

    private WebView mWebView;

    //inflate the layout

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_url, container, false);
    return v;
   }

    @Override
    protected void onViewCreated(View view, Bundle savedInstanceState) {

        mWebView = (WebView) view(R.id.main_webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.gpamravati.ac.in/");
        mWebView.setWebViewClient(new WebViewClient());
    }
}

并且需要在片段内的webview中打开URL所需的基本代码。阅读更多here