我的应用程序使用Google Play服务和FusedLocationCLient来跟踪使用的距离。用户界面只有一个按钮,它将开始跟踪,再次按下将停止跟踪。如果我开始跟踪,然后改变了屏幕方向,则再次按下该按钮会使GPS符号仍显示在屏幕顶部。
在按下按钮时仍在调用正确的方法,但似乎没有达到预期的效果。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
textView = findViewById(R.id.textView);
distanceTracker = new DistanceTracker(this, 10000, 5000, 10, 10);
if (savedInstanceState != null)
{
isTracking = savedInstanceState.getBoolean("is_tracking");
button1.setText(savedInstanceState.getString("button1_text"));
textView.setText(savedInstanceState.getString("textView_text"));
}
else
{
isTracking = false;
}
}
public void buttonClick(View view)
{
if(isTracking)
{
distanceTracker.StopJourney();
isTracking = false;
button1.setText("Stopped Tracking");
}
else {
distanceTracker.StartJourney();
isTracking = true;
button1.setText("Started Tracking");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("is_tracking", isTracking);
outState.putString("button1_text", button1.getText().toString());
outState.putString("textView_text", textView.getText().toString());
super.onSaveInstanceState(outState);
}
当方向更改时,似乎所创建的distanceTracker不在范围内,如何在方向更改后如何保留对首次启动活动时创建的distanceTracker对象的引用。