[英]Android maintain Activity after clicking back button on FragmentActivity
我現在正在開發地理定位APP,但更新的Google 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
在MainActivity
定義一個重寫函數。
@Override
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.