Search code examples
javaandroidoverriding

What is the use of AppComparActivity.class in Android Studio? Can I write my method on it as a parent class?


Is it possible to rewrite (override the existed method) the AppCompatActivity in Android Studio and if yes, what is the best solution to do so?

Thanks a lot


Solution

  • What is the use of AppCompatActivity class in Android?

    straight from the official documentation

    Base class for activities that use the support library action bar features.

    In other words, this is the base class for a 'screen' in android. You can edit the UI components of your screen and apply logic to it, like responding to user clicks for example.
    The AppCompat means that is a class from the support library, that you may integrate with other support components, for better supporting older android versions.

    Can I write my method on it as a parent class?

    As any other (non final) class in Java, you can extend this class and override its public and protected methods. It's recommended to call super super.overridenMethod(params) in every method you override (for not breaking anything).

    You may also write your own methods of course, and use them as you please.

    For getting the maximum from your activity, you should understand the activity lifecycle, in case your methods behave unexpectedly.