-3

I am using Android Studio and have 2 Activities, SearchActivity and RentStartActivity. In my SearchActivity, I have an EditText where a user will click on and it will redirect him to the next Activity. Then in the next Activity, he will select a Date and Time from the CalendarView and TimePicker and when 'Done' Button is clicked, the user will go back to SearchActivity and the Date and Time will be set as a text in the same EditText that he clicked.

Problem is, it returns null.

My code for SearchActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    etRentS = (EditText) findViewById(R.id.etRentS);
    etRentS.setFocusable(false);
    etRentS.setClickable(true);
    etRentS.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent rentS = new Intent(SearchActivity.this, RentStartActivity.class);
            startActivityForResult(rentS, 1);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == 1)
    {
        String date1 = data.getStringExtra("Date1");
        String time1 = data.getStringExtra("Time1");
        etRentS.setText(date1 + time1);
    }
}

And here's my code for RentStartActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rent_start);


    calendar1 = (CalendarView) findViewById(R.id.calendar1);
    time1 = (TimePicker) findViewById(R.id.time1);

    btnDone1 = (Button) findViewById(R.id.btnDone1);
    btnDone1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent done1 = new Intent();
            done1.putExtra("Date1", calendar1.getDate());
            done1.putExtra("Time1", time1.getDrawingTime());
            setResult(1, done1);
            finish();
        }
    });

    btnCancel1 = (Button) findViewById(R.id.btnCancel1);
    btnCancel1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cancel1 = new Intent(RentStartActivity.this, SearchActivity.class);
            startActivity(cancel1);
        }
    });

}

Please help and thank you!! :)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Justin
  • 45
  • 6
  • 2
    Both `CalendarView#getDate()` and `View#getDrawingTime()` return a `long`, not a `String`. Also, `getDrawingTime()` is not what you want for the [`TimePicker`](https://developer.android.com/reference/android/widget/TimePicker.html). – Mike M. Jun 19 '17 at 03:54
  • `getStringExtra` will return null when the data isn't there **or** the key value is not actually a string – OneCricketeer Jun 19 '17 at 03:55
  • Oh then what is the correct way to type it? @MikeM. – Justin Jun 19 '17 at 09:15

2 Answers2

0

in RentStartActivity you have to do setResult(Activity.RESULT_OK, done1);

and in SearchActivity

onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            /* what you were doing */
        }
    }
}
Jay Pandya
  • 138
  • 4
0

You should check result code in if statement -

if(requestCode == 1 && resultCode == 1)
    {

..............
}

This may help you.

San Ko Ko
  • 385
  • 4
  • 11