简体   繁体   English

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

[英]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.

相关问题 Android:FragmentActivity中的“后退”按钮事件 - Android: Back button event in FragmentActivity 单击后退按钮后重新创建活动状态 - Recreating activity state after clicking back button 如何在单击返回按钮并返回同一活动时维护活动的 state - How to maintain the state of an activity at the time of clicking back button and coming back to the same activity Android-从FragmentActivity到另一个Fragment的后退按钮 - Android - Back button from FragmentActivity to another Fragment ANDROID:按下后退按钮后的活动状态 - ANDROID: Activity state after pressing the back button 单击返回键并返回Android活动后,音乐无法播放 - Music does not play after clicking back key and returning to activity in Android 控件进入下一个活动后,保持活动状态,用于后退按钮遍历 - Maintain Activity State, After Control Passes To Next Activity ,For Back Button Traversing 从另一个活动返回后,FragmentActivity调用onCreate - FragmentActivity calls onCreate after coming back from another Activity 单击android中的按钮后如何保持相同的活动? - How to stay in same activity after clicking button in android? 在android中单击警告对话框中的正向按钮后关闭活动 - Closing the activity after clicking positive button in alert dialog in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM