简体   繁体   English

Android:如何仅在首次启动应用程序时才将相机移到我的位置?

[英]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.

相关问题 每次运行应用程序时,是否都使用Java Web Start Application浏览器? - Is java Web Start Application using my browser each time I run the application? 如何检测用户是否是第一次使用我的应用程序? - How can I detect if a user is using my application for the first time? 如何在OpenGL中移动相机 - How to move my camera in OpenGL 如何在没有错误的情况下启动Android SQLite应用程序 - How I can start my Android SQLite Application without a Error 如何为2D游戏移动相机? - How do i move the camera for my 2D game? 我的地图上有2个标记。 如何将相机对准两者而不仅仅是将相机移动到其中一个? - I have 2 marker on my map. How to focus camera on both and not just move camera to one of them? 如何在我的应用程序中启动 Android 应用程序? (提供示例) - How to start an Android application inside my application? (example provided) 如何从我的Android应用程序中启动另一个应用程序 - how to start another application from my application in android 如何在ANdroid中加载应用程序时启动相机? - how to start camera on Loading application in ANdroid? 如何在java中启动Windows启动我的应用程序 - How can I Start My application on Start up of Windows in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM