화면 회전이나 액티티가 죽는 문제를 icepick으로 바삭하게 해결



다른 액티비티에서 지도 화면으로 돌아가거나 화면을 회전하면 현재 위치가 바뀌고 사용하기 쉽지 않다는 리뷰를 볼 수 있습니다. icepick으로 쉽게 해결할 수 있습니다.

Google Maps Activity 선택





키 설정





Google 개발자 콘솔에서 Google Maps Android API 키 가져오기



icepick 설치



LatLng 및 zoom 값을 유지하는 데 필요한 라이브러리.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "jp.co.demo.map"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        //追加
        repositories {
            maven {url "https://clojars.org/repo/"}
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.google.android.gms:play-services:9.6.1'
    testCompile 'junit:junit:4.12'

    //追加
    compile 'frankiesardo:icepick:3.2.0'
    provided 'frankiesardo:icepick-processor:3.2.0'
}


MapsActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import icepick.Icepick;
import icepick.State;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


    @State
    LatLng cLocation = new LatLng(35.658622, 139.745530);

    @State
    float cZoom = 20;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Icepick.restoreInstanceState(this, savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(cLocation,cZoom));
        mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                cZoom = mMap.getCameraPosition().zoom;
                cLocation = mMap.getCameraPosition().target;
                Toast.makeText(getApplicationContext(), "ズーム" + cZoom + ":" + "経緯緯度" + cLocation.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }
}


요약



몇 줄, 몇 분 안에 구현할 수 있으므로 icepick은 편리합니다.

좋은 웹페이지 즐겨찾기