1. Are you ready for the Galaxy S20? Here is everything we know so far!

Best method for sending data from DialogFragment to Activity

Discussion in 'Android Development' started by croninberg, Jun 1, 2017.

  1. croninberg

    croninberg Newbie
    Thread Starter

    I'm currently trying to send data from my Dialog Fragment to an activity, and while there are a number of ways to do it, what is best practice in this case. So far I've seen people setting up an interface within the DF, others have used broadcasts and others again use setTargetFragment with a result code.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Well assuming that the Activity instantiates the Dialog Fragment, then can you not simply use a getter method on the Dialog Fragment?
     
  3. croninberg

    croninberg Newbie
    Thread Starter

    Would you be able to point me to an example of this. While I get what your saying I'm a little confused to how I should implement it.
     
  4. Deleted User

    Deleted User Guest

    Post your code up, showing the Dialog Fragment, and the Activity, and I'll see if it can be modified to do what I suggest.
     
  5. croninberg

    croninberg Newbie
    Thread Starter

    Below is a cut down version of the dialog fragment. In this instance I'm using an interface and passing in a bundle to it. My Activity implements this interface and simply gets the data as its passed in.

    Code (Java):
    1. public class NewItemDialogFragment extends android.support.v4.app.DialogFragment {
    2.  
    3. ...
    4.  
    5.  
    6.     public static NewItemDialogFragment newInstance() {
    7.  
    8.         return new NewItemDialogFragment();
    9.  
    10.     }
    11.  
    12.     @Override
    13.     public void onCreate(@Nullable Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.  
    16.         hasPrice = false;
    17.         hasQuantity = false;
    18.     }
    19.  
    20.     @Nullable
    21.     @Override
    22.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    23.  
    24.         mView = inflater.inflate(R.layout.new_item_dialog_fragment, container, false);
    25.  
    26.         ButterKnife.bind(this, mView);
    27.  
    28.         return mView;
    29.     }
    30.  
    31.     /**
    32.      * Interface that allows parent activity retrieve information from
    33.      * dialog.
    34.      */
    35.     public interface OnCompleteListener {
    36.         void onComplete (final Bundle bundle);
    37.     }
    38.  
    39.     /**
    40.      * Save the item.
    41.      */
    42.     @OnClick(R.id.btn_save)
    43.     protected void save() {
    44.  
    45.         Bundle bundle = new Bundle();
    46.  
    47.         String name = mItemName.getText().toString();
    48.  
    49.         int quantity = 0;
    50.         int price = 0;
    51.         if (!name.equals("")) {
    52.  
    53.             bundle.putString(ITEM_NAME, name);
    54.  
    55.             if (hasQuantity) {
    56.                 quantity = Integer.parseInt(mQuantity.getText().toString());
    57.  
    58.                 bundle.putInt(QUANTITY, quantity);
    59.             }
    60.  
    61.             if (hasPrice) {
    62.                 price = Integer.parseInt(mPrice.getText().toString());
    63.  
    64.                 bundle.putInt(PRICE, price);
    65.  
    66.             }
    67.  
    68.             mOnCompleteListener.onComplete(bundle);
    69.         }
    70.  
    71.         cancel();
    72.     }
    73.  
    74.     @Override
    75.     public void onAttach(Context context) {
    76.         super.onAttach(context);
    77.  
    78.         try {
    79.  
    80.             mOnCompleteListener = (OnCompleteListener) context;
    81.  
    82.         } catch (ClassCastException e) {
    83.             throw new ClassCastException(context.toString() + " must implement NewItemDialogFragment");
    84.         }
    85.     }
    86. }
     
    sweetndreemy73 likes this.
Loading...
Similar Threads - Best method sending
  1. Dodopie Gamer
    Replies:
    2
    Views:
    312
  2. Stuey
    Replies:
    2
    Views:
    517
  3. avneetahuja7
    Replies:
    1
    Views:
    458
  4. Osher
    Replies:
    2
    Views:
    457
  5. Jimmy Recard
    Replies:
    5
    Views:
    680
  6. haniya
    Replies:
    0
    Views:
    709
  7. haniya
    Replies:
    1
    Views:
    429
  8. smfoley1
    Replies:
    3
    Views:
    1,155
  9. persistentone
    Replies:
    12
    Views:
    774
  10. consultant
    Replies:
    2
    Views:
    635

Share This Page

Loading...