1

I have a chronometer in my MainActivity and I wanted to save the elapsed time and pass it to SecondActivity and show the elapsed time and continue running it in second activity using shared preferences but I failed to do so.

I tried using textView to check if the value is passed from main activity to second activity and it does but when it comes to chronometer it doesn't work, The chronometer in second activity remains 0:00.

Here is the code: MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
private Chronometer chronometer;
@Override
      protected void onStart(){
        super.onStart();
          final long startTime = (SystemClock.elapsedRealtime() - chronometer.getBase());
          chronometer.setBase(startTime);
          final SharedPreferences pref = getSharedPreferences("chronometer",Context.MODE_PRIVATE);
          final SharedPreferences.Editor editor = pref.edit();

        mChildReference1.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange (DataSnapshot dataSnapshot){
                    String status = dataSnapshot.getValue(String.class);

                   if (status.equals ("occupied")) {

                        chronometer.start();
                        editor.putLong("time", startTime);
                       editor.commit();

                   }
                   else
                       mValueView1.setBackgroundColor(Color.parseColor("#66d983"));

              }

SecondActivity.java

@Override
    protected void onStart() {
        super.onStart();

        final SharedPreferences pref = getSharedPreferences("chronometer",Context.MODE_PRIVATE);

        mChildReference1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String status = dataSnapshot.getValue(String.class);

                if(status.equals("occupied")) {

                    savedTime = pref.getLong("time",01);
                    timer.setText(String.valueOf(savedTime));
                    chronometer.setBase(savedTime);
                    chronometer.getBase();

                }
                if(!status.equals("occupied")){
                        chronometer.setBase(SystemClock.elapsedRealtime());
                        pauseOffset = 0;
                        chronometer.stop();
                        parkingCharge=0; 

                    }
                }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
Anonymous
  • 74
  • 2
  • 10
  • Does this answer your question? [How to save chronometer time and retrieve it in another activity using sharedpreferences](https://stackoverflow.com/questions/58777436/how-to-save-chronometer-time-and-retrieve-it-in-another-activity-using-sharedpre) – Markus Kauppinen Nov 12 '19 at 14:50
  • _"[...] when it comes to chronometer it doesn't work."_ How exactly it doesn't work? What's the actual problem with the code? – Markus Kauppinen Nov 12 '19 at 14:54
  • 1
    No because now the problem is the chronometer in the second activity does not show the starting time that elapsed in main activity it remains 0:00. I've tried using textview to check if the value is passed to the second activity and it shows some numbers in the textview so I assume it is in miliseconds. So now the question is how am I suppose to do in order to let the chronometer retrieve the starting time from main activity and continue running it in second activity. – Anonymous Nov 12 '19 at 15:02

0 Answers0