4

I am building a simple timer app in which I am using android chronometer to track time passed. but when I start chronometer and change orientation to landscape the chronometer resets and and show 00:00 again. I want it to retain its value. layouts for portrait and landscape are different

portrait--> Layout folder

landscape->layout_land folder

<Chronometer 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:id="@+id/chronometer1"
    android:layout_above="@+id/button2"
    android:textStyle="bold"
    android:textColor="@color/Indigo"
    android:text="Chronometer"
    android:layout_toLeftOf="@+id/save_btn"
    android:typeface="serif"
    android:textSize="40dp"/>
frogatto
  • 28,539
  • 11
  • 83
  • 129
AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • 1
    @ChrisStratton I disagree. Most of the answers to the question advocate the use of `android:configChanges="orientation"` which is definitely the wrong answer. Adil, you should read about `SharePreferences`. Save the current value in `onPause()` and restore it in `onResume()`. – Simon Feb 09 '14 at 15:36
  • @Simon : i got your point thanks :) – AddyProg Feb 09 '14 at 15:38
  • @Simon - please re-read the primary answer of the linked question which is about saving state. Personally though, I'm not sure ignoring orientation changes is the wrong answer. Phones often respond to incidental tilts in ways that do not enhance usability at all. It really requires figuring out what is best in a given use case. – Chris Stratton Feb 09 '14 at 23:44
  • @ChrisStratton The common use of this attribute is like this: Rotate phone, app crashes. Beginner reads about configChanges and adds it to manifest. Rotate phone, app doesn't crash. Job done!. Except the bug that caused the original crash is still there, just hidden, and is waiting for one of the other scenarios when the activity can be destroyed. I have yet to see configChanges used for a genuine reason and the Play Store is littered with apps with these bugs - often seen when the phone rings when the app is open. As a community, we should be promoting coding to the life cycle. It's not hard. – Simon Feb 10 '14 at 08:00
  • @simon - none of that would be any less applicable to the other question than this one. Feel free to post your own answer there, or nominate a better duplicate. – Chris Stratton Feb 10 '14 at 13:43

1 Answers1

2

Here is what the doc said :

Recreating an Activity

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

If you need to Save activity's state before changing orientation, you have to override onSaveInstanceState(). This method will help you save all the values you want to get back after orientation changes. Then restore them in onCreate() method.

Here is an exemple :

String myVar;

/* 
     ...
*/
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save myVar's value in saveInstanceState bundle
    savedInstanceState.putString("myVar", myVar);
    super.onSaveInstanceState(savedInstanceState);
}

Now retrieve it when creating the activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    // savedInstanceState is the bundle in which we stored myVar in onSaveInstanceState() method above
    // savedInstanceState == null means that activity is being created a first time
    if (savedInstanceState == null) {
        // some code here
        myVar = "first value";
    } else {  // savedInstance != null means that activity is being recreated and onSaveInstanceState() has already been called.
        myVar = savedInstanceState.getString("myVar");
    }
    /*
       ...
    */
}
Community
  • 1
  • 1
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52