How to get chronometer elapsed time and continue running in another activity


  • 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.

    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(SystemClock.elapsedRealtime());
              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) {
    
                }
    
            });
    

    Does anyone know the solution? Seriously need help guys 😕



  • Hey @unknown

    I have moved this topic into the Android category. I also recommend sharing this question on facebook and linkedin with a public scope. That way it gets more exposure on the internet and more people can find your question.