반응형
출처: https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity
Try using interfaces.
Any fragment that should pass data back to its containing activity should declare an interface to handle and pass the data. Then make sure your containing activity implements those interfaces. For example:
In your fragment, declare the interface...
public interface OnDataPass {
public void onDataPass(String data);
}
Then, connect the containing class' implementation of the interface to the fragment in the onAttach method, like so:
OnDataPass dataPasser;
@Override
public void onAttach(Context context) {
super.onAttach(context);
dataPasser = (OnDataPass) context;
}
Within your fragment, when you need to handle the passing of data, just call it on the dataPasser object:
public void passData(String data) {
dataPasser.onDataPass(data);
}
Finally, in your containing activity which implements OnDataPass
...
@Override
public void onDataPass(String data) {
Log.d("LOG","hello " + data);
}
반응형
'IT기술 관련 > 모바일' 카테고리의 다른 글
[안드로이드]SQLite date값 9시간 차이나는 문제 해결방법 (0) | 2017.10.06 |
---|---|
[안드로이드] Fragment 에서 Toast 사용하기 (0) | 2017.10.01 |
[안드로이드] EditText에서 String값으로 가져오기 & EditText값이 공백인지 체크하기 (0) | 2017.09.29 |
[Android] Activity에서 Fragment 함수 호출, Fragment에서 Activity 함수 호출 출처: http://kwangsics.tistory.com/entry/Android-Activity에서-Fragment-함수-호출 (0) | 2017.09.28 |
[안드로이드] Fragment 에서 findviewbyid 사용하기 (1) | 2017.09.28 |