LoginSignup
2
3

More than 5 years have passed since last update.

画面回転やアクティティが殺される問題をicepickでサクッと解決する

Last updated at Posted at 2016-12-08

Screenshot_20161208-121844_png.png

別のアクティビティから地図の画面に戻ってきたり、画面回転すると、現在位置が変わって使い勝手が悪いというレビューを見ます。icepickで簡単に解決できます。

Google Maps Activityを選択

Create_New_Project.png

キーの設定

google_maps_api_xml_-_Map_-____Desktop_Android_Dropbox_Map_.png

Google developers consoleで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は便利。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3