public interface NoticeDialogListener { public void onDialogPositiveClick( DialogFragment dialog ); public void onDialogNegativeClick( DialogFragment dialog ); } NoticeDialogListener _listener; @TargetApi(23) @Overridepublic void onAttach( Context context ) { super.onAttach(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { onAttachToContext(context); } } @SuppressWarnings("deprecation") @Overridepublic void onAttach(Activity activity) { super.onAttach(activity); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { onAttachToContext(activity); } } public void onAttachToContext( Context context ) { Activity activity; if( context instanceof Activity ) { activity = (Activity) context; _listener = (NoticeDialogListener) activity; } else { _listener = (NoticeDialogListener) context; } }
Wednesday, January 13, 2016
OnAttach of a Dialog Listener for Version 23 and Below Version 23
I wrestled with this a little until I saw an implementation on the web and another on StackOverflow. I'm posting it here so I can remember where to find it. The version 23 Context method calls the Activity method, hence the check for Build.VERSION.SDK_INT < Build.VERSION_CODES.M, to avoid calling onAttachToContext twice, which results in a crash.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment