0

我想制作一个由 (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)Main FragmentActivity 但丢失了 TextView 字段上的所有数据..

所以我想做的就是

  1. 当第一次单击“返回”按钮时,(2)Google Map V2 FragmentActivity 应该被杀死并且 TextView 字段上的所有数据(在 (1)Main FragmentActivity)应该是活动的。
  2. 当第二次单击“返回”按钮时,应用程序应该被杀死(退出)而没有错误。

谢谢

----------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
4

0 回答 0