본문 바로가기

대외활동/UMC

[UMC 2기] Android 파트 2주차 정리

복습 및 정리

Activity

appication을 구성하는 기본 단위이며 사용자와 상호작용하는 UI를 표시해주는 요소

앱이 실행될 때 화면에 UI를 표시하는 역할

사용자의 클릭이나 입력 등의 이벤트를 처리하는 역할

새로운 UI를 화면에 표시

AppcompatActivity

안드로이드에서 Activity 기능들을 사용할 수 있도록 만들어 둔 클래스

특정 버전부터 지원되는 Action bar를 해당 버전 이하의 버전에도 적용할 수 있도록 지원해주는 Activity

Activity 전환(startActivity)

The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack.

ex)

startActivity(Intent(this, SongActivity::class.java))

++ finish() - activity를 끝내는 역할, stack의 맨 위를 제거

Intent

activity에서 사용하는 택배상자, 어플리케이션 구성요소 간에 작업 수행을 위한 정보를 전달하는 역할을 하는 것

명시적 인텐트(Explicit Intent) - 클래스 객체나 컴포넌트 이름을 지정하여 호출될 대상을 확실히 알 수 있는 경우

암시적 인텐트(Implicit Intent) - 호출될 대상의 속성들을 지정했지만 호출될 대상이 달라질 수 있는 경우

 

Fragment

Fragment represents a reusable portion of your app's UI.

한 화면에 있던 UI 들을 조각이라는 단위로 재배치 하는 것

UI 구성을 모듈 단위로 나누어 독립성을 추가해준다.

Fragment 전환

ex)

(context as MainActivity).supportFragmentManager.beginTransaction().replace(R.id.main_frm,AlbumFragment()).commitAllowingStateLoss()


(context as MainActivity)   // mainActivity안에 있는 fragment를 뜻함

.replace(R.id.main_frm,AlbumFragment())  // main_frm을 id값으로 갖는 것을 AlbumFragment로 전환

.commitAllowingStateLoss()     // 내부적으로 동작하는 부분

 

Binding

예전에는 findViewById를 사용했지만 nullpointerexeption문제, 가독성 문제가 있었다.

→ View Binding이 해결

View binding is a feature that allows you to more easily write code that interacts with views.

ex)

1. Activity Binding

class SongActivity : AppCompatActivity() {
    lateinit var binding : ActivitySongBinding   // lateinit : 초기화를 나중에

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySongBinding.inflate(layoutInflater)
        // inflate : xml의 표기된 레이아웃들을 메모리에 객체화 시키는 행동
        setContentView(binding.root)
        // setContentView : xml을 객체화시키는 Inflate 동작

        binding.songDownIb.setOnClickListener{
            finish()
        }
    }
}

 

2. Fragment Binding

class HomeFragment : Fragment() {
    lateinit var binding: FragmentHomeBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeBinding.inflate(inflater, container, false)

        binding.homeAlbumImgIv1.setOnClickListener{
            (context as MainActivity).supportFragmentManager.beginTransaction().replace(R.id.main_frm,AlbumFragment()).commitAllowingStateLoss()
        }

        return binding.root
    }
}

 

Log

logcat에 기록을 남긴다. → 데이터가 잘 들어갔는지 확인하는 용도로 사용할 수 있다.

Generally, you should use the Log.v(), Log.d(), Log.i(), Log.w(), and Log.e() methods to write logs.

 

Toast

A toast provides simple feedback about an operation in a small popup.

짧은 메시지 형식으로 정보를 전달하는 팝업창

 

참조

https://hongbeomi.medium.com/appcompatactivity-%ED%8C%8C%ED%97%A4%EC%B9%98%EA%B8%B0-4ed4fc231899

https://www.crocus.co.kr/1521

https://developer.android.com/reference/android/util/Log

https://developer.android.com/guide/topics/ui/notifiers/toasts