0

I intend to launch a Chrome Custom Tab from the RecyclerView like this -

public CustomAdapter(Context context, List<Artifact> ListOfArtifacts) {
    this.context = context;
    this.ListOfArtifacts = ListOfArtifacts;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.artifactAuthor.setText(ListOfArtifacts.get(position).getAuthor());
    holder.artifactTitle.setText(ListOfArtifacts.get(position).getTitle());
    holder.seeders.setText(String.valueOf(ListOfArtifacts.get(position).getSeeders()));
    holder.leechers.setText(String.valueOf(ListOfArtifacts.get(position).getLeechers()));
    holder.addedOn.setText(df.format(ListOfArtifacts.get(position).getAdded_on()));
    holder.artifactTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                /*Intent launchArtifactAuthor = new Intent(Intent.parseUri(ListOfArtifacts.get(position).getURL(), Intent.URI_INTENT_SCHEME));
                context.startActivity(launchArtifactAuthor);*/
                CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();

                // Begin customizing
                // set toolbar colors
                intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
                intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));

                // set start and exit animations
                intentBuilder.setStartAnimations(context, android.R.anim.slide_out_right, android.R.anim.fade_in);
                intentBuilder.setExitAnimations(context, android.R.anim.slide_in_left,
                        android.R.anim.slide_out_right);

                // build custom tabs intent
                CustomTabsIntent customTabsIntent = intentBuilder.build();
                customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));


            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Since the method signature of customTabsIntent.launchUrl requires the first parameter to be an Activity, I cast the context into a Activity hence the

java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity

on the line customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));

How do I fix this ?

CodeWalker
  • 2,281
  • 4
  • 23
  • 50

1 Answers1

1

If you will always need activity inside your recycle view, I think there is no point in getting Context and parsing it to Activity; instead just get the Activity from the constructor.

so I suggest changing your constructor to

public CustomAdapter(Activity activity, List<Artifact> ListOfArtifacts) {
    this.activity= activity;
    this.ListOfArtifacts = ListOfArtifacts;
}

and just use activity to launch Chrome custom tabs.

customTabsIntent.launchUrl(activity, Uri.parse(ListOfArtifacts.get(position).getURL()));

in case you are wondering what's wrong with your code, I think you are passing Application context, not Activity context.

humazed
  • 74,687
  • 32
  • 99
  • 138