1

When user clicks on some date from datePicker then, how can i go to previous fragment from current activity. can i store that timestamp into my database actually i want to save that date in some variable and transfer it to my database. Plz help me i'm new in android

date_activity.java

public class DatePick extends AppCompatActivity {
DatePicker datep;
public long date;

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

    datep = findViewById(R.id.datePicker);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    date=calendar.getTimeInMillis();

    Intent intent = new Intent();
    intent.putExtra("date", date);
    setResult(Activity.RESULT_OK, intent);
    finish();
}            

Fragment.java

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 CardView cd= view.findViewById(R.id.card);
    cd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            date_Frag();
        }
    });
 }
   public void date_Frag() {
    Intent intent = new Intent(this.getActivity(), DatePick.class);
    startActivityForResult(intent,11111);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 11111 && resultCode == Activity.RESULT_OK) {
        long date = data.getExtras().getLong("date");
    }
rk_1
  • 106
  • 2
  • 13
  • You can save timestamp using https://developer.android.com/reference/android/content/SharedPreferences.html and if you just want previous fragment call `finish();` – Sunil Sunny Mar 08 '18 at 13:09

1 Answers1

0

If you just want to close the activity (and don't need to send back data), you can simply call finish() in the DatePick activity.


If you want to send back data from the DatePick activity back to your fragment, you can use onActivityResult().

For example, first use startActivityForResult to start your second activity:

Intent i = new Intent(this, DatePick.class);
startActivityForResult(i, 11111);

Send back data from DatePick using setResult:

Intent intent = new Intent();
intent.putExtra("date", date);
setResult(Activity.RESULT_OK, intent);
finish();

And read it back in your fragment:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 11111 && resultCode == Activity.RESULT_OK) {
        long date = data.getExtras().getLong("date");
    }
}

See this for detailed usage examples

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • when i use startactivityforResult then its intent is not working. have Edited my code plz take a look – rk_1 Mar 08 '18 at 15:42
  • You're doing something wrong. `startActivityForResult` must be called in the first activity/fragment. – Sheharyar Mar 08 '18 at 15:44
  • how can i add ok button to that datepicker for confirming the selected date by user. – rk_1 Mar 08 '18 at 18:27