AIK
AIK

Reputation: 528

saving the state of activity when the layout changes

i need to save my all variables and text views to display all values when i change my layout to landscape. can anybody help me out? i tried to save somethings in the onsavedInstanceState method but it won't save anything. here is my code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    accu = (TextView) findViewById(R.id.accu);
    speed1 = (TextView)findViewById(R.id.speed);
    t = (TextView)findViewById(R.id.t);
    prevLatLon = (TextView) findViewById(R.id.prevLatLon);
    listView = (ListView)findViewById(R.id.listView);
    listText = (TextView)findViewById(R.id.listText);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {


public void onLocationChanged(Location loc) {
                  //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,  0, locationListener);
            accuracy = (int) loc.getAccuracy();

            listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.activity_simplelist, R.id.listText, list));

            accu.setText("Accuracy: " + accuracy);
            speed1.setText("Speed: " + speed);
            t.setText("Time: " + time);

            DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Date date = new Date(loc.getTime());
            String formatted = format.format(date);

            if(flag == 0){
                latitude =  loc.getLatitude();
                longitude = loc.getLongitude();
                speed = (int) loc.getSpeed();
                time = formatted;

                flag = 1;
                list.add("latitude: " + latitude + " longitude: " + longitude +
                        " \nspeed: " + speed + " Time: " +time);
            }
            else {
                prevLatitude = latitude;
                prevLongitude = longitude;
                prevSpeed = speed;
                prevTime = time;

                prevLatLon.setText("Previous Latitude: " + latitude + "\nPrevious Longitude: " + longitude +
                                    " \nPrevious speed: " + speed + " \nTime: " + time);
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                speed = (int) loc.getSpeed();
                time = formatted;

                list.add("latitude: " + latitude + " longitude: " + longitude +
                        " \nspeed: " + speed + " Time: " + time);
                speed = prevSpeed + speed;
            }
            /*currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            currentLat = currentLocation.getLatitude();
            currentLon = currentLocation.getLongitude();*/

            //list.add("speed: " + speed + " accuracy: " + accuracy);
            Toast.makeText(MainActivity.this, "Location Changed", Toast.LENGTH_SHORT).show();
        }

i tried this

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
        accu.setText("Accuracy: " + accuracy);
        speed1.setText("Speed: " + speed);
        t.setText("Time: " + time);

        prevLatLon.setText("Previous Latitude: " + latitude + "\nPrevious Longitude: " + longitude +
                " \nPrevious speed: " + speed + " \nTime: " + time);

        savedInstanceState.putDouble("latitude", latitude);
        savedInstanceState.putDouble("longitude", longitude);
        savedInstanceState.putDouble("prevLatitude", prevLatitude);
        savedInstanceState.putDouble("prevLongitude", prevLongitude);
        savedInstanceState.putDouble("speed", speed);
        savedInstanceState.putDouble("accuracy", accuracy);
        // etc.

}

Upvotes: 0

Views: 41

Answers (1)

Tom Tsagkatos
Tom Tsagkatos

Reputation: 1494

When you save something using the onSaveInstanceState you are supposed to retrieve them later on either on the onRestoreInstanceState or using the onCreate(Bundle).

On the onCreate of your activity do this code:

onCreate(Bundle bundle)
{
    /* ... */

    if (bundle == null)
    {
        //Initialize data
        data = 0;
    } else
    {
        //Load data from the bundle (the same way you saved them)
        data = bundle.getInt("MY_DATA", 0); //0 is default value
    }
}

Alternatively you can use the onRestoreInstanceState(Bundle) method which guarantees that bundle will always be non-null. I'm not sure which of the two is better for which case, but both work.

Upvotes: 1

Related Questions