Android “Best Practice” returning values from a dialog

后端 未结 6 1653
闹比i
闹比i 2020-12-07 18:27

What is the \"correct\" way to return the values to the calling activity from a complex custom dialog - say, text fields, date or time picker, a bunch of radio buttons, etc,

6条回答
  •  生来不讨喜
    2020-12-07 19:08

    I'm using following way:

    1. All my activities has one and the same parent Activity (let's say ControlActivity). ControlActivity has private volatile Bundle controlBundle; with appropriate getter/setter
    2. When I start dialog, I used to call dialog thru my own method:

      public void showMyDialog(int id, Bundle bundle)
      {
          this.controlBundle=bundle;
          this.showDialog(id, bundle);
      }
      

    So each time I know parameters sent to dialog

    1. When dialog is about to complete, I'm forming in dialog another Bundle with necessary values and then put them thru my Activity bundle setter:
    
    ((ControlActivity )this.getOwnerActivity).setControlBundle(bundle);
    

    So in the end when dialog finishes I know value "returned" from dialog. I know that it's not like int retCode=this.showMyDialog(); it's a bit more complex, but it's workable.

提交回复
热议问题