Pedro Relvas
Pedro Relvas

Reputation: 679

Android- How to extend AppCompatActivity() in an Adapter?

I got an adapter and I got an action which open a "app camera", then I capture the image. And from here it got to execute a function called onActivityResult(requestCode: Int, resultCode: Int, data: Intent?), but I can't override the method because I can't extend AppCompatActivity().

Any help?

Upvotes: 0

Views: 817

Answers (1)

cutiko
cutiko

Reputation: 10517

You can't, in Java classes can't inherit from two classes at the same time. Only one. Considering your adapter already extends RecyclerView.Adapter then you can't.

The recommended approach is to use callback. A callback is an interface that is implements on the host activity or fragment and pass as an argument to the other class (the adapter on this case). Your adapter will hold a reference to the callback, so when the view is clicked then the reference will be called triggering the implementation on the activity or fragment.

You can see a detailed explanation here

  1. When the click happens, from the activity start the intent to the camera
  2. After the photo is taken inside onActivityResult on the activity pass the result to the adapter
  3. The adapter need a method for receiving the result, it should be something like this
public void addPhoto(SomeObject object){
    yourData.add(object);
    notifyDataseChanged();
}
  1. The code inside onActivityResult should be something like this
SomeObject objet = data... //you have to get the data and transform it to your format
adaper.addPhoto(object)

Upvotes: 1

Related Questions