ProcessCameraProvider.BindToLifecycle Using Xamarin.Android Activity Instead of AppCompatActivity

348 views Asked by At

A large percentage of examples that I find online inherit from AppCompatActivity rather than Activity. Is this really what I should be doing? I have heard that Activity is newer, and it is my preference. I am currently attempting to learn to use CameraX, and there are very few examples & support that I have been able to find for Xamarin.Android. After finding the following Android tutorial & a translation of it for Xamarin.Android:

https://developer.android.com/codelabs/camerax-getting-started

https://github.com/DottorPagliaccius/Xamarin.CameraX

However, the first parameter of BindToLifecycle seems to like AppCompatActivity but not Activity. Does anybody know what changes I need to make in order to be able to use Activity instead of AppCompatActivity?

1

There are 1 answers

4
Jianwei Sun - MSFT On

Actually, AppCompatActivity is newer than Activity. AppCompatActivity supports some of the newer platform features on older Android devices. Some of these backported features include: Using the action bar, including action items, navigation modes and more with the setSupportActionBar(Toolbar) API.

But don't be worry about trouble of using AppCompatActivity.It's similar to usage of Activity. You said that the first parameter of BindToLifecycle seems to like AppCompatActivity but not Activity. In fact, you can treat it like Activity to use.

        // Bind use cases to camera
        cameraProvider.BindToLifecycle.
        (this, cameraSelector, preview, imageCapture, imageAnalyzer);

For more details about AppCompatActivity and Activity, you can check: https://stackoverflow.com/questions/31297246/activity-appcompatactivity-fragmentactivity-and-actionbaractivity-when-to-us#:~:text=The%20differences%20between%20them%20are%3A%20Activity%20is%20the,Based%20on%20FragmentActivity%2C%20AppCompatActivity%20provides%20features%20to%20ActionBar.