0

I'm facing an issue regarding intent in RecyclerView. I just want intent on 1 imageView that is in my RecyclerView.

Error is:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:1383) at android.app.ContextImpl.startActivity(ContextImpl.java:1370) at android.content.ContextWrapper.startActivity(ContextWrapper.java:323) at example.memories.MyAdapter$ViewHolder$1.onClick(MyAdapter.java:75) at android.view.View.performClick(View.java:4871) at android.view.View$PerformClick.run(View.java:20365) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:6102) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 07-13 01:55:31.033 20699-29708/example.memories E/NativeCrypto: ssl=0x5578a74560 cert_verify_callback x509_store_ctx=0x7f9844d228 arg=0x0 07-13 01:55:31.033 20699-29708/example.memories E/NativeCrypto: ssl=0x5578a74560 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA

and my RecyclerView Adapter code is:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context context;
    private List<Upload> uploads;
    Upload upload;

    public MyAdapter(Context context, List<Upload> uploads) {
        this.uploads = uploads;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_images, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
         upload = uploads.get(position);

       holder.textViewName.setText(upload.getName());

        Glide.with(context).load(upload.getUrl()).into(holder.imageView);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public ImageView imageView,shareImage;


        public ViewHolder(View itemView) {
            super(itemView);

           textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            shareImage=(ImageView)itemView.findViewById(R.id.share);

            shareImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(upload.getUrl()));
                    shareIntent.setType("image/*");
                   context.startActivity(Intent.createChooser(shareIntent, "Share memories:"));

                }
            });
        }

    }

Kindly guide me. how can i achieve this?

Muhammad Saad
  • 713
  • 1
  • 9
  • 31

2 Answers2

0

Either pass in an Activity context instead of an Application context when you construct MyAdapter, or add the following line before you call context.startActivity:

shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Krause
  • 4,689
  • 1
  • 21
  • 25
0

Don't pass getApplicationContext() to your adapter. Just pass your activity's context and you don't even need to shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); do this.

And beware when using application context. I really suggest you to read Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34