0

In my MainActivity:

public class MainActivity extends AppCompatActivity{
public static final int REQUEST_TIME = 1;

.....

public void chooseTime(){
    ImageButton btn_time = (ImageButton)findViewById(R.id.btn_time);
    btn_time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent_time = new Intent(MainActivity.this,time_pick.class);
            startActivityForResult(intent_time,REQUEST_TIME);
        }
    });
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case REQUEST_TIME: {
            if(requestCode == RESULT_OK){
                TextView text_time = (TextView)findViewById(R.id.menu_time);
                text_time.setText(data.getStringExtra("time_detail"));
            }
        }
    }
}

In my TimePicker's activity:

public void onButtonClick(){
    Button btn = (Button)findViewById(R.id.btn_time_confirm);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker) ;
            String time_detail = timePicker.getCurrentHour() + " : " + timePicker.getCurrentMinute();
            Intent data = new Intent();
            data.putExtra("time_detail",time_detail);
            setResult(RESULT_OK,data);
            finish();
        }
    });
}

I tried to start my TimePicker activity from MainActivity, and get the time value from the TimePicker and display in one of my TextView in MainActivity.

That's the code, I used putExtra and getStringExtra but I doesn't work, the activity did change from MainActivity to TimePicker's activity and from TimePicker's activity to MainActivity when I click on the buttons.

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
Jusle
  • 13
  • 5
  • Did you put breakpoints to see the different values? – Eselfar Jan 10 '18 at 15:20
  • @Eselfar How can it be done? I'm new in programming. – Jusle Jan 10 '18 at 17:10
  • Read [this page from the documentation](https://developer.android.com/studio/debug/index.html). Then put a breakpoint in the onActivityResult to see if you get what you expect. – Eselfar Jan 10 '18 at 17:35

0 Answers0