[앱만들기 프로젝트 with 구글 코드랩] #2-1 : Activities와 Intent
진도표
Lesson 2: Activities and intents
요약
Activity:
- Activity는 앱의 하나의 스크린을 의미하는데 사용자가 그것으로 하나의 테스크를 수행할 수 있다. 메일보내기, 사진 찍기, 지도보기와 같이.
- 앱 시작할 때 사용자에게 보이는 화면을 main activity.
- main activity는 다른 행위를 수행하기 위하여 다른 activities를 시작한다.
- 이전 activity 는 stack의 뒷부분으로 간다. ‘last in, first out.’ 이전 것은 스텍의 안으로 들어가고, 새로운 것은 stack의 바깥으로 나오는 원리이다. 사용자가 현재 액티비트를 끝내고 Back 버튼을 누르면 스텍에서 이전 액티비티가 다시 수행된다.
- Activity는 하나의 사용자 테스트에 초점을 맞춘 하나의 스크린을 제공하는 앱 컴포넌트다.
- 각각의 액티비티는 사용자 인터페이스 레이아웃 파일을 갖는다.
- 앱에서 Up navigation을 활성화하여 Activity 를 부모-자식 관계로 구현할 수 있다.
- View는 android:visibility 속성으로 보이거나 안보이게 할 수 있다.
인텐트:
- 액티비티는 인텐트(Intent)로 시작되거나 활성화된다.
- 인텐트는 다른 액티비티나 다른 앱 컴포넌트로부터 액션을 요청하기 위한 액티비티에서 사용할 수 있는 비동기적 메시지다.
- 앱에서 다른 컴포넌트로부터 하나의 액션을 요청하도록 한다. 인텐드는 explicit(명시적)하거나 implicit(내제된)하다.
- Explitcit intent 는 인텐트의 타겟을 알 수 있는 인텐트. 특정 액티비티의 자격을 갖춘 클레스 이름을 이미 알고 있다. / 데이터를 받기 위한 특정 타겟 컴포넌트를 의미한다.
- Implicit intent는 타겟 콤퍼넌트의 이름을 가지고 있지 않은 인텐트지만 수행할 액션은 가지고 있다. / 이것으로 원하는 기능을 지정(specify할 수는 있지만 타겟 컴포넌트는 지정할 수 없다.
>> explicit - implicit 의 의미를 명확히 파악해야겠다.
(+) Android 개발자사이트 국문 페이지를 보게되었다. explicit: 명시적/ implcit: 암시적 으로 번역하였구나. (2019-10-27)
- 명시적 인텐트는 인텐트를 충족하는 애플리케이션이 무엇인지 지정합니다. 이를 위해 대상 앱의 패키지 이름 또는 완전히 자격을 갖춘 구성 요소 클래스 이름을 제공합니다. 명시적 인텐트는 일반적으로 앱 안에서 구성 요소를 시작할 때 씁니다. 시작하고자 하는 액티비티 또는 서비스의 클래스 이름을 알고 있기 때문입니다. 예를 들어, 사용자 작업에 응답하여 새로운 액티비티를 시작하거나 백그라운드에서 파일을 다운로드하기 위해 서비스를 시작하는 것 등이 여기에 해당됩니다.
- 암시적 인텐트는 특정 구성 요소의 이름을 대지 않지만, 그 대신 수행할 일반적인 작업을 선언하여 다른 앱의 구성 요소가 이를 처리할 수 있도록 해줍니다. 예를 들어 사용자에게 지도에 있는 한 위치를 표시하고자 하는 경우, 암시적 인텐트를 사용하여 해당 기능을 갖춘 다른 앱이 지정된 위치를 지도에 표시하도록 요청할 수 있습니다.
Task 1: Create the TwoActivities project
클릭을 하면 (onClick), 두번 째 액티비티가 런칭되도록 코드를 넣었는데, 빨간색으로 표시되었다.
아직 launchSecondActivity()가 생성되지 않았기 때문.
The attribute value is underlined in red because the launchSecondActivity() method has not yet been created. Ignore this error for now; you fix it in the next task.
해결을 위해 MainActivity에서 다음 코드를 추가한다.
Run을 눌렀는데 메모리가 부족하다고 뜨기도 하고, anti-virus 프로그램이 영향을 줄 수도 있다고 에러가 뜬다.
Task 2: Create and launch the second Activity
[내용 정리하기]
Each new activity you add to your project has its own layout and Java files, separate from those of the main activity. They also have their own elements in the AndroidManifest.xml file. As with the main activity, new activity implementations that you create in Android Studio also extend from the AppCompatActivity class.
Each activity in your app is only loosely connected with other activities. However, you can define an activity as a parent of another activity in the AndroidManifest.xml file. This parent-child relationship enables Android to add navigation hints such as left-facing arrows in the title bar for each activity.
An activity communicates with other activities (in the same app and across different apps) with an intent. An Intent can be explicit or implicit:
- An explicit intent is one in which you know the target of that intent; that is, you already know the fully qualified class name of that specific activity.
- An implicit intent is one in which you do not have the name of the target component, but have a general action to perform.
In this task you add a second activity to our app, with its own layout. You modify the AndroidManifest.xml file to define the main activity as the parent of the second activity. Then you modify the launchSecondActivity() method in MainActivity to include an intent that launches the second activity when you click the button.
고유의 레이아웃을 가진 세컨드 액티비티를 추가하고, second activity의 부모로서 메인엑티비티를정의하기위하여 AndroidManifest.xml 파일을 수정한다.
2.1 Create the second Activity
- Click the app folder for your project and choose File > New > Activity > Empty Activity.
며칠 전 잠이 덜 깨서인지, 이 메뉴를 못찾아서 새로운 Project를 만들어 헤맸지. 그래서 다음 단계에서 언급한 항목들은 더더욱 찾을 수가 없었다. 역시 졸릴 때는 자야해.
2. Name the new Activity SecondActivity. Make sure Generate Layout File and Backwards Compatibility (AppCompat) are checked. The layout name is filled in as activity_second. Do not check the Launcher Activity option.
Click Finish. Android Studio adds both a new Activity layout (activity_second.xml) and a new Java file (SecondActivity.java) to your project for the new Activity. It also updates the AndroidManifest.xml file to include the new Activity.
하나의 프로젝트에서 두번째 액티비티를 추가하면 다음과 같이 새로운 java 파일이 추가된다.
layout에도 activity_second.xml이라는 xml파일이 생성되었다.
activity_second.xml 에서 다음과 같이 설정하고,
TextView의 텍스트는 여기에 입력해야하는구나!
마지막으로 mainActivity.java에서 인텐드 추가.
이렇게 하면 Send 버튼을 누를 시, MainActivity가 Intent를 보내 SecondActivity가 런치되도록 해야하는데...