HS_development_log

Android - Intent 본문

Android

Android - Intent

DevHyeonseong 2020. 7. 17. 19:53
반응형

 

1. 인텐트(Intent)

 

안드로이드 애플리케이션은 4대 컴포넌트(Component)로 구성됩니다.

 

Android - Component

Component 하나의 독립적인 형태로 존재합니다. Intent를 통해서 상호작용합니다. 각 Component들은 각자 고유의 기능을 수행합니다. 1. Activity Activity는 사용자에게 제공되는 UI가 있는 화면입니다. 한마

hyeonseong.tistory.com

이때 컴포넌트간의 상호작용 수단을 하는 것이 인텐트(Intent) 입니다.

 


2. 명시적(explicit) 인텐트(Intent)

 

  • 컴포넌트를 정확히 지칭하여 컴포넌트를 활성화 합니다.

  • 일반적으로 본인의 앱 안에서 컴포넌트를 시작할 때 사용됩니다.

2.1 예시

Intent intent = new Intent(A_Activity.this, B_Activity.class);
startActivity(intent);

 

1) A액티비티에서 B액티비티를 시작하면서 데이터를 전달할때는

Intent intent = new Intent(A_Activity.this, B_Activity.class);

intent.putExtra("key", "value");

startActivity(intent);

2) B액티비티 클래스 에서 데이터를 받을때는

Intent intent = getIntent();
if(intent!=null){
	String value = intent.getStringExtra("key");
}

3) A액티비티 클래스에서 B를 호출하고 결과를 리턴 받고 싶을때는 

Intent intent = new Intent(A_Activity.this, B_Activity.class);
startActivityForResult(intent, 100);

startActivityForResult로 인텐트와, 요청코드를 매개변수로 B액티비티를 호출합니다.

 

Intent intent = new Intent();
intent.putExtra("key", "value");
setResult(RESULT_OK, intent);
finish();

B액티비티에서 인텐트에 데이터를 담아서 종료합니다.

 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==100 && data!=null){
    	String str = data.getStringExtra("Key");
    }

A액티비티에서 onActivityResult 메서드를 오버라이딩해서 결과를 전달받습니다.

 


3. 암시적(implicit) 인텐트(intent)

 

  • 활성화할 패키지명과 컴포넌트명 없이 컴포넌트의 동작 만으로 활성화됩니다.

  • 컴포넌트의 이름을 대지 않았지만, 수행할 동작을  선언하여 또 다른 앱의 컴포넌트가 이를 처리할때 쓰입니다.

  • 웹 브라우저 호출, 이메일 전송, 전화앱으로의 통화 등등이 있습니다.


3.1. 예시

1) 전화걸기 인텐트를 호출 하고 싶다면

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("tel:010-0000-0000"));
startActivity(intent);

 

반응형

'Android' 카테고리의 다른 글

Android Design Pattern - MVVM : Model, View, ViewModel  (0) 2020.09.28
Android Design Pattern - MVP : Model, View, Presenter  (0) 2020.08.29
Android - View , ViewGroup  (0) 2020.07.17
Android - Thread  (0) 2020.07.17
Android - Activity Lifecycle  (0) 2020.06.29