0

I haven't seen any cases like this. I have an activity which is initiated from a button on a dialog. I need to get a variable from this activity, close it, and pass it back to the dialog. What would be my approach?

 class ColorPickDialog(val activity: Activity, color: Int, val callback: (color: Int) -> Unit) {
lateinit var savedColorsButton: Button
val currentColorHsv = FloatArray(3)

init {
    Color.colorToHSV(color, currentColorHsv)

    val view = activity.layoutInflater.inflate(R.layout.d_colorpicker, null).apply {

        savedColorsButton = findViewById(R.id.saved_colours_button)

    savedColorsButton.setOnClickListener{
        val intent = Intent(this.activity.applicationContext, DisplayColorsActivity::class.java)
        intent.putExtra("SettingState", true)

        this.activity.applicationContext.startActivity(intent)
    }


 }

This is the activity that the dialog opens.

 public class DisplayColorsActivity extends Activity {
 public void displayColors() {
    ArrayList<ColourRGB> coloursList = colourStorage.getColours();

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        btn = (Button) findViewById(R.id.select_color_btn);
        if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("SettingState")) {
            btn.setVisibility(View.VISIBLE);
        }
        else {
            btn.setVisibility(View.INVISIBLE);
            Log.v("Status+", "INot there" );

        }
   } 
    public void selectButtonClicked(View view){
    finish();
    }
 }

I need to pass a variable from the DisplayColorsActivity back to the ColorPick dialog, maybe in the selectButtonClicked fuction, (which takes you back to the dialog using a button) Please note the first snippet is in Kotlin, the second in Java

David Wasser
  • 93,459
  • 16
  • 209
  • 274
pam
  • 113
  • 1
  • 10
  • `ColorPickDialog` is displayed from a activity? Can you not pass back values to that activity and then to the dialog? – Raghunandan Apr 04 '18 at 13:40
  • Yes is it displayed from another activity. How could I pass it back to it? Should I create an intent extra and handle it in the same way as I passed values to the 'DisplayColorsActivity' from the dialog? I need the dialog to stay open on the previous activity. – pam Apr 04 '18 at 13:53
  • you should check this https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – Raghunandan Apr 04 '18 at 13:54
  • That seems like what I need, will try it out, thank you! – pam Apr 04 '18 at 13:56

1 Answers1

0

declare a constant of your choice:

static final int COLOR_PICKED_REQUEST = 1234;

in your dialog replace

this.activity.applicationContext.startActivity(intent)

with

this.startActivityForResult(intent, COLOR_PICKED_REQUEST);

and add a method:

public void userPickedColor(int color){
    Log.d("TAG", "COLOR:"+color);
}

in your DisplayColorsActivity add:

Intent intentMessage=new Intent();   
intentMessage.putExtra("COLOR",valueOfYourColor);
setResult(COLOR_PICKED_REQUEST,intentMessage);

above the finish();

in the activity that is holding your dialog make sure you have a reference to the dialog itself like mActionDialog and then override:

@Override
protected void onActivityResult(int requestCode, int resultCode, 
Intent data) {
    if (requestCode == COLOR_PICKED_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            int color = data.getIntExtra("COLOR"); 
            mActionDialog.userPickedColor(color);
        }
    }
}
bko
  • 1,038
  • 1
  • 9
  • 17
  • This seems great, but onActivityResult overrides nothing, neither in the dialog or the activity that holds it – pam Apr 04 '18 at 16:24
  • @pam You mean that its not getting called in the activity that holds the dialog? – bko Apr 04 '18 at 17:35
  • No, it does not. It does not even seem to exist. Then again, maybe it is because the startActivityForResult is being called in the dialog – pam Apr 04 '18 at 18:19
  • My bad, I had some items that did not translate well from Kotlin to Java – pam Apr 04 '18 at 19:34