当我将布局更改为横向时,我需要保存所有变量和文本视图以显示所有值。任何人都可以帮助我吗?我试图在onsavedInstanceState方法中保存一些东西,但它不会保存任何东西。这是我的代码。
@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();
}
我试过这个
@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.
}
答案 0 :(得分:1)
当您使用onSaveInstanceState
保存某些内容时,您应该稍后在onRestoreInstanceState
上或使用onCreate(Bundle)
检索它们。
在您的活动的onCreate
上执行以下代码:
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
}
}
或者,您可以使用onRestoreInstanceState(Bundle)
方法保证bundle始终为非null。我不确定在哪种情况下哪两个更好,但两者都有效。