Android:活动并没有相互传递额外的东西

时间:2016-02-21 20:59:53

标签: android android-intent android-activity onactivityresult

我正在尝试将一些字符串从A传递到B和B传递给A,但由于某种原因它不起作用。这是我尝试做的事情:

在A中,我有两个全局变量:minuteValuehourValue,它们默认为00.当我从B传递一些字符串时,它们会更新,但名称相同但值不同到A.无论交换是否发生,我都将这些值从A传递给B.

在B中,我只会在用户按下按钮时传递这些值:timerButton并且这是在setUpTimerButton方法中完成的

似乎我从未进入A中的onActivityResult方法,因为我有打印语句而我从未见过它们。

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);

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;
        }
    });
}

我知道timerButton有效,因为我在单击按钮时打印出值,但由于某些原因我从未进入onActivityResult,我认为这是真正的问题......

2 个答案:

答案 0 :(得分:3)

从A到B时,

startActivityForResult而不是startActivity。如果你有从A到其他屏幕的不同导航,你可以使用这个构造函数并使用requestCode < / p>

public void startActivityForResult(Intent intent, int requestCode)

答案 1 :(得分:1)

startActivity(timerActivity);

这行代码是罪魁祸首 你必须使用这个:

startActivityForResult(Intent intent, int requestCode);

能够在B中使用setResult(int result)