在我最近编程的Android应用程序中,我发现了一个关于Chrome自定义标签的问题。我正在使用自定义标签打开不同社交媒体平台的网站,如Twitter,Twitch,YouTube,Instagram,Snapchat和Facebook。
我将快速概述我的应用程序的活动,以便您了解我的问题:
仪表板活动 -> 后活动 -> 打开自定义选项卡
当我使用自定义选项卡打开Twitter页面并单击顶部的关闭按钮时,我将被重定向到PostActivity。这也发生在Twitter,YouTube和其他平台上。
但是,当我使用自定义选项卡打开Facebook页面并关闭选项卡时,尽管我使用相同的代码段来启动它以启动其他页面,但我仍被重定向到DashboardActivity。
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
switch (view.getId()) {
case R.id.youtube_content_image: {
//START YOUTUBE APP
YouTubePost youtubePost = (YouTubePost) mPosts.get(position);
String url = "http://www.youtube.com/watch?v=" + youtubePost.getId();
builder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.color_youtube));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
}
case R.id.instagram_content_image: {
InstagramPost post = (InstagramPost) mPosts.get(position);
builder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.color_instagram));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(post.getWebLink()));
}
case R.id.snapchat_content_text: {
SnapchatPost post = (SnapchatPost) mPosts.get(position);
builder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.color_snapchat));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(post.getSnapchatUrl()));
}
case R.id.facebook_content_text: {
FacebookPost post = (FacebookPost) mPosts.get(position);
String url = "https://www.facebook.com/" + post.getId();
builder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.color_facebook));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
}
case R.id.twitter_content_text: {
TwitterPost post = (TwitterPost) mPosts.get(position);
String url = "https://twitter.com/" + mAccountHolder.getTwitterUsername() +"/status/" + post.getId();
builder.setToolbarColor(ContextCompat.getColor(getApplicationContext(), R.color.color_twitter));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
}
}
发布的代码缺少每种情况的 break 语句。在 Java 中,如果你不中断,switch 语句就会失败,这意味着,所有低于第一个匹配的情况也会被执行。这可能会生成意外错误。