0

My application only displays the map on Google maps. My problem is when I change the position of my screen (the application I installed on my tablet), turning it there's a white screen that appears before displaying the map.

My configuration; res / layout / main.xml and res / layout-land / main.xml to switch from portrait to landscape mode.

What should add or do to resolve this behavior?

Nathan Rice
  • 3,022
  • 1
  • 18
  • 30
mdev
  • 11
  • 2
  • can you post your activity's onCreate and layouts? let's see if you're doing some excessive work there. – Eugen Pechanec Mar 10 '15 at 20:45
  • edit your question. there's not enough room in comments – Eugen Pechanec Mar 10 '15 at 22:09
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { initilizeMap(); } catch (Exception e) { e.printStackTrace(); } } – mdev Mar 10 '15 at 22:09
  • private void initilizeMap(){ if (googleMap == null) { googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); if (googleMap == null) { Toast.makeText(getApplicationContext(), "impossible de créér googleMaps", Toast.LENGTH_LONG).show(); } } } @Override protected void onResume(){ super.onResume(); initilizeMap(); } – mdev Mar 10 '15 at 22:11

2 Answers2

0

Since you have specified the map fragment in the layout file, you can safely find it using findFragmentById in onCreate like so:

MapFragment mMapFragment; // this is neither GoogleMap or MapView!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
    initializeMap();
}

@Override
public void onResume() {
    super.onResume();
    // map init was already done in onCreate, will not do it twice
}

Then when you need the GoogleMap object you ask for it and have it delivered via a callback. When the callback executes, the map is guaranteed to be not null. This will work since Google Play services library v6.5.

private void initializeMap() {
    mMapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            // googleMap will never be null here
            // do not store it in a variable, always make a getMapAsync call
            // do your map setup here
        }
    }
}

This is as quick as it gets so you shouldn't experience any flickering.

Note: In the original post you mentioned you had different layouts for portrait and landscape. If you really do, make sure both contain corresponding fragment IDs, otherwise you're bound to get exceptions. You also mentioned the layout was main.xml not activity_main.xml so make sure you use the layout you intended.

Eugen Pechanec
  • 34,848
  • 7
  • 94
  • 115
  • Is there any difference between your portrait and landscape layout? Please update the question with formatted code, don't post it in comments, it gets hard to read. – Eugen Pechanec Mar 11 '15 at 07:52
0

I solved my problem reading this (Activity restart on rotation Android) Activity restart on rotation Android

Community
  • 1
  • 1
mdev
  • 11
  • 2