bob dylan
bob dylan

Reputation: 677

Android: Activities are not passing extras back and forth with each other

I am trying to pass some strings from A to B and B to A, but it is not working for some reason. Here is what I try doing:

In A, I have two global variables: minuteValue and hourValue, and they are defaulted to 00. They are updated when I pass the some strings from B, with the same names but different values, to A. Regardless of whether the exchange happens, I pass those value from A to B.

In B, I only pass those values if the user presses a button: timerButton and this is done in the setUpTimerButton method

It seems that I never go into my onActivityResult method in A since I have print statements and I never see any of them.

Code for A:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (resultCode == RESULT_OK) {

                if(requestCode == 3){
                //the values for the timer
                String hourValue = data.getStringExtra("hourValue");
                String minuteValue = data.getStringExtra("minuteValue");

                //find the values from the timer if the user had set it earlier
                if (hourValue != null) { this.hourValue = hourValue;
                    System.out.println("HOUR IS " + hourValue ); }
                if (minuteValue != null) { this.minuteValue = minuteValue;
                    System.out.println("minute IS " + minuteValue); }
                else{
                    System.out.println("timer minute value is NULL");
                }
            }
    }

...
//going from A to B
Intent timerActivity = new Intent(MainActivity.this, CustomTimer.class);
                            timerActivity.putExtra("minuteValue", minuteValue);
                            System.out.println("passing back");
                            timerActivity.putExtra("hourValue", hourValue);
                            System.out.println("passing back hour");

                    startActivityForResult(timerActivity, 3);

Code for B:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_timer);

    hourValue = (EditText) findViewById(R.id.hour_value);
    minuteValue = (EditText) findViewById(R.id.minute_value);
    timerButton = (Button) findViewById(R.id.start_timer_button);

    //grabs the values from A 
    if(getIntent().getStringExtra("hourValue") != null)
        hourValue.setText(getIntent().getStringExtra("hourValue"));
    if(getIntent().getStringExtra("minuteValue") != null)
        minuteValue.setText(getIntent().getStringExtra("minuteValue"));
    else{
        System.out.println("could not find values");//this never occurs
    }

    setUpHourValue();
    setUpMinuteValue();
    setUpTimerButton();
}

private void setUpTimerButton(){

    timerButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            //startTime = SystemClock.uptimeMillis();
           // handler.postDelayed(updateTimerThread, 0);

            System.out.println("TIMER CLICKED************");
            //send the list of audio titles back to the main activity
            Intent intent = getIntent();
            System.out.println("**** hour and minute values are: " +
                hourValue.getText().toString() + " : " + minuteValue.getText().toString());

            intent.putExtra("hourValue", hourValue.getText().toString());
            intent.putExtra("minuteValue", minuteValue.getText().toString());
            setResult(RESULT_OK, intent);
            finish();//go back to the previous page
        }
    });
}

private void setUpHourValue(){

    hourValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            hourValue.setText(v.getText().toString());
            return false;
        }
    });
}

private void setUpMinuteValue(){

    minuteValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            minuteValue.setText(v.getText().toString());
            return false;
        }
    });
}

I know the timerButton works since I print out the values when the button is clicked, but I never go into onActivityResult for some reason and I think that's the real problem...

Upvotes: 0

Views: 61

Answers (2)

Moonbloom
Moonbloom

Reputation: 7918

startActivity(timerActivity);

That line of code is the culprit here.
You have to use this:

startActivityForResult(Intent intent, int requestCode);

To be able to use setResult(int result) in B.

Upvotes: 1

Mithun
Mithun

Reputation: 2075

startActivityForResult instead of startActivity while going from from A to B. You can use this constructor, and use requestCode if you have different navigations from A to some other screens as well

public void startActivityForResult(Intent intent, int requestCode)

Upvotes: 3

Related Questions