The "Android Notes" show the Fragment map

Source: Internet
Author: User

This article from: fair-jm.iteye.com, please specify the source

The official website of the tutorial is under the activity under the fragment in the Gold forum to find some ways to try to display but there will always be some problems after the switch

For example, after the switch is new, the map doesn't show up after the switch.

This way I can keep the map state after switching, but I have to limit the screen to horizontal or vertical if the flip will also error

Layout file:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent" >5 6   <Com.amap.api.maps.MapView7     xmlns:android= "Http://schemas.android.com/apk/res/android"8 Android:id= "@+id/mapview"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Match_parent" > One   </Com.amap.api.maps.MapView> A  - </Relativelayout>

Java code:

1  Packagecom.cc.android.map.fragment;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.util.Log;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.View;9 ImportAndroid.view.ViewGroup;Ten  One ImportCom.amap.api.maps.AMap; A ImportCom.amap.api.maps.MapView; - Importcom.cc.android.map.MainActivity; - ImportCOM.CC.ANDROID.MAP.R; the Importcom.cc.android.map.constant.Constants; -  -  Public classMapfragmentextendsFragment { -    +   Private StaticMapfragment fragment=NULL; -    Public Static Final intPosition=0; +    A   PrivateMapview Mapview; at   PrivateAMap AMap; -   PrivateView maplayout; -    -    Public StaticFragment newinstance () { -     if(fragment==NULL){ -       synchronized(Mapfragment.class){ in         if(fragment==NULL){ -Fragment=Newmapfragment (); to         } +       } -     } the     returnfragment; *   } $ @OverridePanax Notoginseng    PublicView Oncreateview (layoutinflater inflater, ViewGroup container, - Bundle savedinstancestate) { the     if(Maplayout = =NULL) { +LOG.I ("sys", "MF Oncreateview () null"); AMaplayout = Inflater.inflate (R.layout.map,NULL); theMapview =(Mapview) Maplayout.findviewbyid (R.id.mapview); + mapview.oncreate (savedinstancestate); -       if(AMap = =NULL) { $AMap =Mapview.getmap (); $       } -}Else { -       if(Maplayout.getparent ()! =NULL) { the ( (ViewGroup) maplayout.getparent ()). Removeview (maplayout); -       }Wuyi     } the     returnmaplayout; -   } Wu  - @Override About    Public voidOnattach (activity activity) { $     Super. Onattach (activity); - ( (mainactivity) activity). onsectionattached (constants.map_fragment); -   } - @Override A    Public voidonCreate (Bundle savedinstancestate) { +     Super. OnCreate (savedinstancestate); the      -   } $  the @Override the    Public voidOnresume () { theLOG.I ("sys", "MF Onresume"); the     Super. Onresume (); - Mapview.onresume (); in   } the  the   /** About * method must be overridden the * Map's life cycle approach the    */ the @Override +    Public voidOnPause () { -LOG.I ("sys", "MF OnPause"); the     Super. OnPause ();Bayi mapview.onpause (); the   } the  -   /** - * method must be overridden the * Map's life cycle approach the    */ the @Override the    Public voidonsaveinstancestate (Bundle outstate) { -LOG.I ("sys", "MF onsaveinstancestate"); the     Super. Onsaveinstancestate (outstate); the mapview.onsaveinstancestate (outstate); the   }94  the   /** the * method must be overridden the * Map's life cycle approach98    */ About @Override -    Public voidOnDestroy () {101LOG.I ("sys", "MF OnDestroy");102     Super. OnDestroy ();103 Mapview.ondestroy ();104   } the}

This ensures that the map does not display or restore when you switch fragment

Also note that the app's orientation in the list does not turn the screen upside down

In the Activity tab, write:

1 android:screenorientation= "Portrait"

Reference Original: Http://www.tuicool.com/articles/FnqYRf

"Android Notes" displays the fragment map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.