2021. 4. 23. 18:59ㆍAndroid/개발 CASE
Activity ↔︎ Activity : Intent.putExtra()
Activity → Fragment : Bundle.putString() / fragment.setArguments(bundle)
Fragment → Activity : Interface 이용(best) or
// Passing data from Fragment .
Bundle gameData = new Bundle();
gameData.putStringArrayList(Constant.KEY_PLAYERS_ARR,players);
gameData.putString(Constant.KEY_TEAM_NAME,custom_team_name);
gameData.putInt(Constant.KEY_REQUESTED_OVER,requestedOver);
Intent intent = getActivity().getIntent();
intent.putExtras(gameData);
// Getting data from the bundle from it's container activity .
Bundle gameData=getIntent().getExtras();
if(gameData!=null){
int over=gameData.getInt(Constant.KEY_REQUESTED_OVER);
ArrayList<String> players=gameData.getStringArrayList(Constant.KEY_PLAYERS_ARR);
String team=gameData.getString(Constant.KEY_TEAM_NAME);
}else if(gameData==null){
Toast.makeText(this,"Bundle is null",Toast.LENGTH_SHORT).show();
}
참고 url : https://guides.codepath.com/android/using-dialogfragment
setTargetFragment
Added in API level 11
Deprecated in API level 28
public void setTargetFragment (Fragment fragment, int requestCode)
Optional target for this fragment. This may be used, for example, if this fragment is being started by another, and when done wants to give a result back to the first. The target set here is retained across instances via FragmentManager#putFragment.
Parameters
- fragment
Fragment: The fragment that is the target of this one.
- requestCode
int: Optional request code, for convenience if you are going to call back with onActivityResult(int, int, android.content.Intent).
public class CommonJoinMoreFragment extends BaseFragment implements View.OnClickListener {
mEventBottomSheetDialogFragment = new EventBottomSheetDialogFragment();
mEventBottomSheetDialogFragment.setTargetFragment(this, REQ_BOTTOM_DIALOG);
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_BOTTOM_DIALOG:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String year = String.valueOf(bundle.getInt("year"));
binding.editBirth.setText(year);
binding.redLine.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.line_not_focused));
editFullText();
}
break;
default:
break;
}
}
}
public class EventBottomSheetDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_cancel:
dismissAllowingStateLoss();
break;
case R.id.btn_confirm:
if (getTargetFragment() != null) {
Intent i = new Intent().putExtra(YEAR_KEY, binding.np.getValue());
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
} else {
if (getActivity() instanceof EmailLoginFindActivity) {
((OnApplySelectedListener) getActivity()).onApply(String.valueOf(binding.np.getValue()));
}
}
dismissAllowingStateLoss();
break;
default:
break;
}
}
/**
* Activity 데이터 전달
**/
public interface OnApplySelectedListener {
void onApply(String year);
}
}
public class EmailLoginFindActivity extends BaseActivity implements View.OnClickListener, EventBottomSheetDialogFragment.OnApplySelectedListener {
@Override
public void onApply(String year) {
mBinding.birthEmailLine.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.line_not_focused));
mBinding.birthEmailEdit.setText(year);
mBinding.birthEmailImg.setVisibility(View.INVISIBLE);
possibleToFind();
}
}
'Android > 개발 CASE' 카테고리의 다른 글
[ERROR/EXCEPTION] WebView CrossThreadException (0) | 2021.06.17 |
---|---|
RxJava 같은 리소스로 여러번 발행하기 (0) | 2021.06.17 |
ChildView에 대한 ViewTreeObserver는 같다 (0) | 2021.06.17 |
[ERROR/EXCEPTION] (WIP) Firebase Messaging Service Error (0) | 2021.04.23 |
[ERROR/EXCEPTION] Mediaplayer 파일 재생 불가(내부저장소 접근 불가) + 내/외부 저장소 접근 (0) | 2021.04.23 |