Android关闭片段活动并返回初始显示

时间:2013-10-09 00:07:31

标签: android android-activity fragment

我想创建一个由(1)Main FragmentActivity +(2)Google Map V2 FragmentActivity组成的应用程序。

然而,以下代码在我单击Button * btn1时成功显示了初始(1)Main FragmentActivity和(2)Google Map V2 FragmentActivity。

然而,问题是..当我点击Android上的“后退”按钮时,它成功杀死了2)Google Map V2 FragmentActivity并显示(1)主FragmentActivity但丢失了TextView字段上的所有数据..

所以我想做的就是

  1. 当首先点击“后退”按钮时,(2)Google Map V2 FragmentActivity应该被终止,TextView字段上的所有数据(在(1)Main FragmentActivity中)应该是活着的。
  2. 当第二次单击“后退”按钮时,应用程序shuold被杀死(退出),没有错误。
  3. 感谢

    ---------- -------------- MainActivity.java

    public class MainActivity extends FragmentActivity {
    
        TextView site1_lat, site1_lon, site2_lat, site2_lon, distance_kilo, distance_miles, azimuth1_content, azimuth2_content;
        TextView current_lat, current_lon, current_mgrs_result;
        TextView distance_kilo1, distance_kilo2,    azimuth1_content1, azimuth1_content2;
        Variables vari;
    
        //for geoCoord
        //Context mContext;
        LocationManager mLocMan;
        String mProvider;
        Location location;
        Context context;
        //double latitude, longitude;
        @Override
        public void onBackPressed() {
            //setContentView(R.layout.activity_main);
            this.recreate();
            //this.getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.mapview)).commit();
            //this.getSupportFragmentManager().popBackStack();
            //this.getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.mapview)).commit();
            //this.getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.mapview)).commit();
    
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //for geoCoord
            /*
            mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            mProvider = mLocMan.getBestProvider(new Criteria(), true);
            LocationListener mListener = new Geocoord();        
            mLocMan.requestLocationUpdates(mProvider, 6000, 10, mListener);
            */
            //
            mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            mProvider = mLocMan.getBestProvider(new Criteria(), true);
            LocationListener mListener = new Geocoord();
            //mLocMan.requestLocationUpdates(mProvider, 0, 0, mListener);
            mLocMan.requestLocationUpdates(mLocMan.GPS_PROVIDER, 6000, 10, mListener);
    
            /*
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
             */
    
            //for google Map
            GoogleMap mGoogleMap;
    
    
        //Button 0
        final Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
    
            //1st site
            try{
            EditText mgrs_site1 = (EditText)findViewById(R.id.editText1);
            String site1 = mgrs_site1.getText().toString().toUpperCase();
            .........
    
            }catch(Exception e){
                Toast.makeText(MainActivity.this, "Please input valid Coordination!", Toast.LENGTH_SHORT).show();}//ends try-catch      
        }
        }); //ends button0
    
        //Button 1
        Button btn1 = (Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                btn.performClick();
                //setting up google Map!
                GoogleMap mGoogleMap;
                Converter converter = new Converter();
    
                //initial values for marker
                //current position
                ...
    
                  setContentView(R.layout.mapview);           
                  mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();
                  mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                  //add marker
                  mGoogleMap.addMarker(new MarkerOptions().position(position).title("current")).showInfoWindow();
                  mGoogleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.site1)).position(position1).title("Site#1")).showInfoWindow();
                  mGoogleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.site2)).position(position2).title("Site#2")).showInfoWindow();
    
                  //
                  mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position1, 7));
    
    
                //Intent intent = new Intent(MainActivity.this, MapView.class);
                //startActivity(intent);
            }//ends onClick 
    
    
    
            });
    
        }//ends onCreate
    
    }//ends Activity
    
    //Things to do
    

0 个答案:

没有答案