简体   繁体   中英

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.

The problem is: When I clicking the button1(see the code), it successfully shows the map.

However, when I clicking the back button on the Android device, it immediately kills the APP rather than close the setContentView(R.layout.mapview);

So I would like to show the initial display after clicking the back button rather than kill the APP.

Please help:

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM