Last active
August 29, 2015 14:07
-
-
Save runemart/b3e3778ec9663852837d to your computer and use it in GitHub Desktop.
DialogFragment with constructor arguments, the proper way, that also handles fragment re-creation by Android OS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreditCardDialogFragment extends DialogFragment { | |
public CreditCardDialogFragment() { | |
// Empty constructor required for DialogFragment | |
} | |
public static CreditCardDialogFragment newInstance(CreditCard creditCardOriginal) { | |
final Bundle args = new Bundle(1); | |
args.putSerializable(EXTRA_CREDIT_CARD, creditCardOriginal); | |
CreditCardDialogFragment fragment = new CreditCardDialogFragment(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
creditCardOriginal = (CreditCard) getArguments().getSerializable(EXTRA_CREDIT_CARD); | |
try { | |
listener = (OnCreditCardUpdateListener) getTargetFragment(); | |
} catch (ClassCastException e) { | |
throw new ClassCastException("Calling Fragment must implement OnCreditcardUpdateListener"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment