[英]Android maintain Activity after clicking back button on FragmentActivity
I'm now on developing geolocation APP but updated google API(Google MAP v2) is really confusing. 我现在正在开发地理定位APP,但更新的Google API(Google MAP v2)确实令人困惑。
The problem is: When I clicking the button1(see the code), it successfully shows the map. 问题是:当我单击button1(请参见代码)时,它成功显示了地图。
However, when I clicking the back button on the Android device, it immediately kills the APP rather than close the setContentView(R.layout.mapview);
但是,当我单击Android设备上的“后退”按钮时,它会立即杀死该APP,而不是关闭setContentView(R.layout.mapview);
So I would like to show the initial display after clicking the back
button rather than kill the APP. 因此,我想在单击“ back
按钮后显示初始显示,而不是取消APP。
Please help: 请帮忙:
MainActivity.java 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
Define an overriding function in your MainActivity
. 在MainActivity
定义一个重写函数。
@Override
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.