Android Intent Share无法正常运行

时间:2018-07-05 05:35:00

标签: android android-intent

我是android应用程序开发的新手。 这是场景。
1. 主要活动
2. 第二项活动
3. RecyclerView适配器已连接到第二活动

我正在从主活动导航到包含卡片列表的第二个活动。

每张卡都有一个共享按钮。

当我单击“共享”按钮而不是打开“意图选择器”时,它导航到“主要活动”(不是返回“主要活动”而是重新加载)

这是我的onBindViewHolder方法中的代码

holder.share.setOnClickListener(new 
View.OnClickListener() 
{
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT, "Random Text");
                intent.setType("text/plain");
                context.startActivity(Intent.createChooser(intent, "Send To"));
            }
});

上下文是从“第二个活动”传递的。

问题1:
代码结构有问题吗?
问题2:
我是否应该将这段代码放在onBindViewHolder方法中?
问题3:
是否有其他方法可以在RecyclerView Adapter中实现共享意图?

这是我的适配器代码:

    public class FavQuoteRecyclerView extends RecyclerView.Adapter<FavQuoteRecyclerView.ViewHolder>
    {
        private List<Quote> list;
        private Context ctx;
        private SharedPreferences sp;
        private SharedPreferences.Editor editor;
        private String jsonList;
        private Layout_to_Image layout_to_image;
        private ConstraintLayout constraintLayout;
        private Bitmap bitmap;

    public FavQuoteRecyclerView(List<Quote> list, Context context)
    {
        this.list = list;
        this.ctx = context;
        sp = context.getSharedPreferences(Constants.shared_preference_name, 0);
        editor = sp.edit();
    }

    @NonNull
    @Override
    public FavQuoteRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(ctx).inflate(R.layout.row_quote, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FavQuoteRecyclerView.ViewHolder holder, int position)
    {
        Quote quote = list.get(position);
        holder.favQuote.setText(quote.getQuote());
        holder.favAuthor.setText(quote.getAuthor());
        holder.favCat.setText(quote.getCat());
        holder.favFavButton.setLiked(true);
        Picasso.with(ctx).load(quote.getUrl()).noPlaceholder().into(holder.favBGPic);
        holder.favShareButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                constraintLayout = v.findViewById(R.id.fav_main_constraint_layout_id);
                layout_to_image = new Layout_to_Image(ctx, constraintLayout);
                bitmap = layout_to_image.convert_layout();

                String bitmapPath = MediaStore.Images.Media.insertImage(ctx.getContentResolver(), bitmap, "Title", "share it maximum");
                Uri bitmapUri = Uri.parse(bitmapPath);
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(Intent.EXTRA_TEXT,"bitmapUri");
                ctx.startActivity(Intent.createChooser(sharingIntent,"Title"));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView favQuote;
        public TextView favAuthor;
        public TextView favCat;
        public LikeButton favFavButton;
        public ImageView favBGPic;
        public ImageView favShareButton;

        public ViewHolder(final View view)
        {
            super(view);
            favQuote = view.findViewById(R.id.fav_quote_id);
            favAuthor = view.findViewById(R.id.fav_author_id);
            favCat = view.findViewById(R.id.fav_cat_id);
            favFavButton = view.findViewById(R.id.fav_remove_fav_button);
            favBGPic = view.findViewById(R.id.fav_bg_pic);
            favShareButton = view.findViewById(R.id.fav_share_button);

            favFavButton.setOnLikeListener(new OnLikeListener() {
                @Override
                public void liked(LikeButton likeButton) {
                }

                @Override
                public void unLiked(LikeButton likeButton) {
                    list.remove(getAdapterPosition());
                    notifyItemRemoved(getAdapterPosition());
                    jsonList = new Gson().toJson(list);
                    editor.putString(Constants.list_of_fav_quote, jsonList);
                    editor.commit();
                }
            });
        }
    }
}

这是我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vipercorporation.myquotes">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

        <activity android:name=".FavQuoteActivity"></activity>
    </application>

</manifest>

0 个答案:

没有答案
相关问题