-1

I want to pass the variables year, month, and day to another Activity.

How can I do that?

Button button;
int year_x,month_x,day_x;
static final int DIALOG_ID = 0;


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

    final Calendar cal = Calendar.getInstance();
    year_x = cal.get(Calendar.YEAR);
    month_x = cal.get(Calendar.MONTH);
    day_x = cal.get(Calendar.DAY_OF_MONTH);

    showDialogOnButtonClick();
}


public void showDialogOnButtonClick(){

    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(DIALOG_ID);
                }
            }
    );
}


@Override
protected Dialog onCreateDialog(int id){

    if(id == DIALOG_ID)
        return new DatePickerDialog(this,dpickerListner,year_x,month_x,day_x);
    return null;
}
private DatePickerDialog.OnDateSetListener dpickerListner
        = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        year_x = year;
        month_x = monthOfYear + 1;
        day_x = dayOfMonth;
        Toast.makeText(MainActivity.this,year_x +"/"+ month_x+"/"+day_x,Toast.LENGTH_LONG).show();
    }
};

This is how I start the Activity which shows a CalendarView and DatePicker

public void onButtonClick(View view){
    if(view.getId()==R.id.button2)
    {
        Intent i = new Intent(MainActivity.this,CalendarView.class);
        startActivity(i);
    }
}
}
Rohit Suthar
  • 2,635
  • 1
  • 22
  • 27
MJ_
  • 63
  • 1
  • 1
  • 10
  • 1
    Welcome to StackOverflow! PLEASE TURN OFF THE CAPS LOCK. Thank you. – OneCricketeer Feb 06 '16 at 08:21
  • 2
    Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – Mike M. Feb 06 '16 at 08:26

2 Answers2

2

I don't have enough reputation to comment that's why i am answering your question here.

So first create a pojo class to hold your data you want to pass to next activity .

Create an object of that pojo class and put it in the intent object you created here :

Intent i = new Intent(MainActivity.this,CalendarView.class);
        startActivity(i);

i.e your code should now look like as follows :

Intent i = new Intent(MainActivity.this,CalendarView.class);
i.putExtra("data", yourPojoObj);
        startActivity(i);

thats it. Make sure that your pojo class implements Serializable interface from java.

  • Alternatively, `startActivityForResult` works for passing data back from CalendarView, which I believe is what is being asked for in the question – OneCricketeer Feb 06 '16 at 08:50
  • thanks for the reply @Mrunalini Pawar. I don't know what is a pojo class I'm new to programming but I will search for it thanks for the reply. – MJ_ Feb 06 '16 at 08:50
  • @MJS - Plain Old Java Object = POJO – OneCricketeer Feb 06 '16 at 08:50
  • Guyz sorry if I can't reply immediately it maybe funny but I'm so new to android need to study the answers heheh Hope you understand guyz . – MJ_ Feb 06 '16 at 09:01
0

Activity 1:

    int yr, month, day, hour;
    public void getCurrentDate() {  //Consider getting date from Device
        Calendar today = Calendar.getInstance();
        yr = today.get(Calendar.YEAR);
        month = today.get(Calendar.MONTH);
        day = today.get(Calendar.DAY_OF_MONTH);
        hour = today.get(Calendar.HOUR_OF_DAY);
    }
Intent i=new Intent(Activity1.this,Activity2.class);
i.putExtra("year", yr);
i.putExtra("Month ", month);
startActvity(i);

Activity 2:

 Intent mIntent = getIntent();
 int yrr= mIntent.getIntExtra("year", 0);
 int monthh= mIntent.getIntExtra("Month", 0);
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48