1

I am trying to create a basic application that just counts how many times the user changes the orientation to landscape from portrait and displays the count on screen. I have:

public class MainActivity extends Activity {
int count = 0;
private static boolean inLandscape = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.count);
    tv.setText(getResources().getString(R.string.count) + ' ' + count);
    if (!inLandscape && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        inLandscape = true;
        count++;
        Log.e("Debug","In Landscape " + count);
    }
    else if (inLandscape && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        inLandscape = false;
}

The problem is, in my debug statement count is always 1, and the text never changes on the screen. Doing some research, I think this is because the activity gets trashed and recreated every time the orientation changes. How do I maintain the variable value throughout orientation change?

I tried using the savedInstanceState with

if (savedInstanceState.containsKey("count"))
            count = savedInstanceState.getInt("count");
savedInstanceState.putInt("count", count);

but this gives a NullPointerException at the containsKey line.

awestover89
  • 1,713
  • 4
  • 21
  • 37
  • see http://stackoverflow.com/questions/8248274/android-detect-orientation-changed to detect orient change – Shayan Pourvatan Jan 08 '14 at 06:09
  • I don't want to use Orientation Change events for this project, since I'm using the layout-land and layout-port files. I'd rather just let Android handle the orientation change. – awestover89 Jan 08 '14 at 20:05

2 Answers2

1

Override onSaveInstanceState to store your orientation change counter.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("count", count)
}

and override onRestoreInstanceState to read it back after a change.

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    count = savedInstanceState.getInt("count");
}

Obviously, you can still keep your code in the same place in 'onCreate', for conditionally adding to the counter depending on the orientation each time onCreate is called.

Sound Conception
  • 5,263
  • 5
  • 30
  • 47
1

It's giving you a NullPointerException because saveInstanceState is null. You also need to put the savedInstanceState.putInt("count", count) inside your onRestoreInstanceState method. So your code would look like...

int mCount;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.count);
    mCount = 0;
    tv.setText(getResources().getString(R.string.count) + ' ' + mCount);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mCount++;
    tv.setText(getResources().getString(R.string.count) + ' ' + count);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
    mCount = savedInstanceState.getInt("count");
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("count", mCount);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Please see http://developer.android.com/training/basics/activity-lifecycle/recreating.html and http://developer.android.com/guide/topics/resources/runtime-changes.html for more information.

anthonycr
  • 4,146
  • 1
  • 28
  • 35