728x90

 몇년 사이에 신규로 프로젝트를 생성한다면, AppCompatActivity부터 마주하는 분들이 있을 것 같습니다. 그러나 저처럼 오래된 프로젝트 코드를 마주하면, Activity와 AppCompatActivity를 함께 발견할 수 있습니다.

 


AppCompatActivity는 안드로이드의 하위버전을 지원하는 Activity의 일종입니다.


 

  Activity에서 사용하는 메소드의 API Level을 확인하면, 메소드에 따라 안드로이드 운영체제의 버전이 다른 경우가 존재합니다. (이건 비단 Activity뿐만이 아니라 모든 컴포넌트들에 해당되는 이야기겠지요.) 

  Android 3.0(API level 11)부터, 기존 디폴트 테마를 사용하는 모든 Activitiy들은 app bar로 ActionBar를 가지고 있습니다. 그러나 app bar에는 다양한 안드로이드 버전이 릴리즈되면서 점진적으로 새로운 특징들이 추가 되었습니다. 그 결과, 네이티브 액션바는 기기가 지원할 수 있는 안드로이드 시스템의 버전에 따라 이를 다르게 의존하는 형태로 바뀌게 됩니다. 이와 대조적으로 최신 특징들은 툴바의 support library 버전에 추가 되면서, support lirary를 사용할 수 있는 어느 기기에서도 사용 가능하게 됩니다.

 

  물론 최신기능만이 탑재된 최신 OS 버전의 앱만 만든다면 상관없겠지만, 혹시라도 국내보다 구형기계를 많이 사용하는 지역, 즉 해외 수출용으로 모든 운영체제에 100% 지원이 되어야한다면? 타겟으로 하는 OS 이외에도 하위버전을 얼마나 커버하는 지를 확인 해야 합니다. 

 

  예전에는 경우에 따라 이 Support Library의 버전도 확인해야 했습니다. 그러나 이제는 Support Library가 아닌 AndroidX로 개별 개발자의 고생이 많이 덜어지도록 전환되었기 때문에, 이 부분만 알고 잘 연결한다면 Acitivty에 대한 문제는 거의 없지 않을까 싶네요.

 

  따라서 여러 하위버전을 매번 확인하기 번거롭고, 혹시나하여 안전하게 지원하고 싶다면, AppCompatActivity는 항상 옳은 선택입니다. 

 

디저트가 너무 많아...

 

 Activity를 AppCompatActivity로 전환하기 위해서는 

 1) extends에서 AppCompatActivity로 변경

 2) AndoridManifest.xml에서 해당 클래스를 아래처럼 변경

1
2
3
4
5
6
7
<activity
    android:name="변경 액티비티 경로"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:windowSoftInputMode="stateHidden" />
 
cs

theme은 다음 설정을 내포하고 있습니다.

1
2
3
4
    <style name="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
cs

 

 

 

그렇다면, Acitivty는 deprecated된 것일까요? 아닙니다. Activity는 모든 종류의 Activity의 Base입니다. Acitvity의 상속을 받는 관계를 '부모<-자식'으로 표현했을 때 다음과 같습니다.

 

Activity <- FragmentActivity <- AppCompatActivity <- ActionBarActivity

 

 물론 사실 이 Activity 또한 Context라는 super class로부터 시작합니다. 이에 대한 설명은 https://stackoverflow.com/questions/31297246/activity-appcompatactivity-fragmentactivity-and-actionbaractivity-when-to-us 에서 Farhana님의 답글이 명쾌했으므로, 이를 참고 하시면 좋을 것 같습니다. 아래는 해당 부분입니다.

...더보기

If you talk about Activity, AppcompactActivity, ActionBarActivity etc etc..

We need to talk about Base classes which they are extending, First we have to understand the hierarchy of super classes.

All the things are started from Context which is super class for all these classes.

Context is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc

Context is followed by or extended by ContextWrapper

The ContextWrapper is a class which extend Context class that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context.

Now we Reach to Activity

The Activity is a class which extends ContextThemeWrapper that is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you

Below Classes are restricted to extend but they are extended by their descender internally and provide support for specific Api

The SupportActivity is a class which extends Activity that is a Base class for composing together compatibility functionality

The BaseFragmentActivityApi14 is a class which extends SupportActivity that is a Base class It is restricted class but it is extend by BaseFragmentActivityApi16 to support the functionality of V14

The BaseFragmentActivityApi16 is a class which extends BaseFragmentActivityApi14 that is a Base class for {@code FragmentActivity} to be able to use v16 APIs. But it is also restricted class but it is extend by FragmentActivity to support the functionality of V16.

now FragmentActivty

The FragmentActivity is a class which extends BaseFragmentActivityApi16 and that wants to use the support-based Fragment and Loader APIs.

When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.

ActionBarActivity is part of the Support Library. Support libraries are used to deliver newer features on older platforms. For example the ActionBar was introduced in API 11 and is part of the Activity by default (depending on the theme actually). In contrast there is no ActionBar on the older platforms. So the support library adds a child class of Activity (ActionBarActivity) that provides the ActionBar's functionality and ui

In 2015 ActionBarActivity is deprecated in revision 22.1.0 of the Support Library. AppCompatActivity should be used instead.

The AppcompactActivity is a class which extends FragmentActivity that is Base class for activities that use the support library action bar features.

 

 

 

Reference

 

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading

stackoverflow.com

 

일반 Activity와 AppCompatActivity의 차이가 무엇인가요?

안드로이드에 일반 Activity가 있고, AppCompatActivity가 있던데 그 둘의 차이가 무엇인가요?이거 외에도 AppCompat이 붙은 것이 몇가지 더 있는것 같던데, 차이가 뭔지 모르겠네요.

hashcode.co.kr

 

With API 28 and "androidx.appcompat" library project says "AppCompatActivity" symbol not found

I updated my build and target version to 28 (Pie) and replaced the relevant dependencies. Now my project says Symbol not found on AppCompatActivity. I have tried to Clean project Rebuild project

stackoverflow.com

 

Image Refernece

 

Here’s the first developer preview of Android P – TechCrunch

Just like in the last two years, Google is using the beginning of March to launch the first developer preview of the next version of Android. Android P, as it’s currently called, is still very much a work in progress and Google isn’t releasing it into its

social.techcrunch.com

 

728x90

+ Recent posts