나의 플랫폼/안드로이드

[Android] Get current fragment

GsBOB 2016. 1. 21. 14:03

현재 보여지고 있는 Fragment를 가지고 오고자 할 경우 아래 함수가 정말 유용하다.


Activity에 Fragment를 넣을 시, replace나 add 함수를 통하여 특정 레이아웃에다가 설정을 하였을 것이다.

ex>

getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();


이럴 경우 아래 함수를 통하여 현재 Fragment를 가지고 올 수 있다.



Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);


R.id.fragment_container 라는 레이아웃에 포함된 Fragment를 찾아 준다.



참고하세요.