4

我想从 Fragment 连接 Facebook 和 Twitter,但是当我从 Facebook 或 Twitter 回来时,应用程序已关闭。我想留在那个应用程序中。

我的代码在这里...

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_rssitem_detail,container, false);
     Button facebook=(Button)view .findViewById(R.id.facebook);
            facebook.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Some text");
                 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"http://www.gogole.com");

        final PackageManager pm = v.getContext().getPackageManager();
        final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
        for (final ResolveInfo app : activityList) {
        if ("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        startActivity(shareIntent);
        break;
        }}
        }
        });

    Button twitter=(Button)view.findViewById(R.id.twitter);
        twitter.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Some text");
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "www.google.com");
        final PackageManager pm = v.getContext().getPackageManager();
        final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo app : activityList) {
              if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    startActivity(shareIntent);
                    break;
                  }
                }
            }
        });

return view;
}
} 
4

1 回答 1

0

我的问题在这里得到解决是我在清单文件中的实际代码

<activity
           android:name="com.example.AdslistScreen"
           android:label="@string/app_name" 
           android:noHistory="true"
           android:screenOrientation="portrait"></activity>

由于这条线android:noHistory="true" 我以前的历史记录将在我调用 facebook 时被清除,所以当我回到我的应用程序时,它将关闭整个应用程序。现在我删除这个 android:noHistory="true"线我的代码工作正常。

谢谢大家

于 2013-08-26T06:09:25.650 回答