[英]Android: How to move camera to my location only on each time I first start my application?
I am developing an android app which includes Google maps api. 我正在开发一个包含Google Maps API的Android应用。 Camera is moved to my current location whenever I start the activity. 每当我开始活动时,相机就会移到我的当前位置。 Even after going to some other activity on return it agains starts to point to my current location. 即使返回时进行了其他活动,它也再次指向我当前的位置。 I want to move camera only on the first time when I start the application. 我只想在第一次启动应用程序时移动相机。 I want not to move camera after returning from some other activity. 从其他活动返回后,我不想移动相机。 How can it be implemented? 如何实施?
Currently, I declared a boolean var shouldMove
on top and in the onCreate
method, set its value to true. 当前,我在顶部声明了一个布尔型var shouldMove
,并在onCreate
方法中将其值设置为true。 And after that onLocationChanged
method, I put an if statement ie if(shouldMove)
tk check the variable value. 然后在onLocationChanged
方法之后,我放置了一条if语句,即if(shouldMove)
tk检查变量值。 If it's true then move the camera otherwise not. 如果属实,则移动相机,否则不移动。 So in case of true, first move the camera and then I set its value to false. 因此,如果为true,请先移动相机,然后将其值设置为false。 But whenever I switch to another activity and return back to mapsActivity
, its value is again set tk true, which I don't want to. 但是每当我切换到另一个活动并返回mapsActivity
,它的值就会再次设置为tk true,我不想这样做。
Keep the variable 'shouldMove' not in the activity class. 使变量“ shouldMove”不在活动类中。 In some other static class (for example, Application). 在其他一些静态类(例如Application)中。 There, and change the value of the variable, then it will not depend on the Activity. 在那里,并更改变量的值,那么它将不依赖于Activity。
Example: 例:
Create Application class: public class MyApplication extends Application { private boolean shouldMove; 创建应用程序类:公共类MyApplication扩展了Application {private boolean shouldMove;
public boolean isShouldMove() {
return this.shouldMove;
}
}
in your Activity: 在您的活动中:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
MyApplication myApp=(MyApplication)this.getApplication();
if(myApp.isShouldMove()) {
...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.