Android在FragmentActivity上单击后退按钮后维护Activity

时间:2013-09-22 14:11:26

标签: android api map

我现在正在开发地理位置APP,但更新的谷歌API(Google MAP v2)确实令人困惑。

问题是:当我点击button1(参见代码)时,它会成功显示地图。

然而,当我点击Android设备上的后退按钮时,它会立即杀死APP,而不是关闭setContentView(R.layout.mapview);

所以我想在点击back按钮后显示初始显示,而不是杀死APP。

请帮助:

MainActivity.java

public class MainActivity extends FragmentActivity {

    LocationManager mLocMan;
    String mProvider;
    Location location;
    Context context;
    //double latitude, longitude;

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

        mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mProvider = mLocMan.getBestProvider(new Criteria(), true);
        LocationListener mListener = new Geocoord();

        mLocMan.requestLocationUpdates(mLocMan.GPS_PROVIDER, 6000, 10, mListener);


    //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();
            setContentView(R.layout.mapview);
            //Intent intent = new Intent(MainActivity.this, MapView.class);
            //startActivity(intent);
        }//ends onClick 


        });

    }//ends onCreate

}//ends Activity

1 个答案:

答案 0 :(得分:0)

MainActivity中定义覆盖功能。

@Override
public void onBackPressed() {
    setContentView(R.layout.activity_main);
}