从MapActivity中选择Address,然后返回MainActivity

时间:2018-07-28 11:22:51

标签: android dictionary

我最近开始进行Android开发,我想创建一个表单,通过该表单我也可以获取用户地址。在地址字段中,我放置了一个按钮,可以使用地图选择其位置,我成功获取了位置,但是问题是,当我返回主活动时,我所有的字段都变为空,例如姓名,电话,电子邮件等。

我希望我的所有字段在选择地图位置后都保持填充状态

这是我的主要活动

public class MainActivity extends AppCompatActivity {

    private EditText Name, Phone, Destination;
    private Button mBtnAdd;
    private String type = "1";
    TextView textView;
    LocationManager locationManager;
    String lattitude,longitude;
    private static final int REQUEST_LOCATION = 1;
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name = (EditText) findViewById(R.id.name);
        Phone = (EditText) findViewById(R.id.phone);
        Destination = (EditText) findViewById(R.id.destinationAddress);
        Intent i = getIntent();
        String address = i.getStringExtra ( "address");
        Destination.setText(address);

    }

    private void pickup() {
            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            intent.putExtra ( "map_type", "1" );
            startActivity(intent);
    }

}

这是我在主地图活动中选择的位置功能

public void updateLocation(View view) throws IOException {

        final EditText location = (EditText) findViewById(R.id.name);
        LatLng center = mMap.getCameraPosition().target;
        Log.e(TAG, "Center: " + center);
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());

        addresses = geocoder.getFromLocation(center.latitude, center.longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        Log.e(TAG, "addresses: " + addresses);
        String address = addresses.get(0).getAddressLine(0);
        Intent i = getIntent();
        Intent intent = new Intent( MapsActivity.this, MainActivity.class );
        intent.putExtra ( "address", address );
        startActivity(intent);

    }

1 个答案:

答案 0 :(得分:4)

您需要在onSaveInstanceState中实现方法onRestoreInstanceStateMainActivity

@Override
protected void onSaveInstanceState(Bundle state) {

    /* putting the current instance state into a Bundle */
    state.putString(InstanceStateKeys.CURRENT_NAME, this.Name.getText());
    state.putString(InstanceStateKeys.CURRENT_PHONE, this.Phone.getText());
    state.putString(InstanceStateKeys.CURRENT_DEST, this.Destination.getText());

    super.onSaveInstanceState(state);
}

@Override
protected void onRestoreInstanceState(Bundle state) {

    super.onRestoreInstanceState(state);

    /* and then obtain these values from the Bundle again */
    String currentName  = state.getString(InstanceStateKeys.CURRENT_NAME);
    String currentPhone = state.getString(InstanceStateKeys.CURRENT_PHONE);
    String currentDest  = state.getString(InstanceStateKeys.CURRENT_DEST);

    /* in order to update the GUI with these values */
    if(currentName != null) {this.Name.setText(currentName);}
    if(currentPhone != null) {this.Phone.setText(currentPhone);}
    if(currentDest != null) {this.Destination.setText(currentDest);}
}

其中InstanceStateKeys只是具有一些String常量的类,例如:

public class InstanceStateKeys {
    public static final String CURRENT_NAME = "name";
    public static final String CURRENT_PHONE = "phone";
    public static final String CURRENT_DEST = "dest";
}

实际上,您最好使用.startActivityForResult()来启动MapsActivity,然后在MainActivity的{​​{3}}实现中处理结果。例如。为了传回目标地址。

放在一起看起来可能像这样...

/** start the {@link MapsActivity} for a result: */
private void startMapsActivity() {
    Intent intent = new Intent(this.getContext(), MapsActivity.class);
    intent.putExtra ("map_type", 1);
    this.startActivityForResult(intent, RequestCodes.REQUESTCODE_PICK_DESTINATION);
}

然后,为了将该结果返回到MainActivity

public void updateLocation(View view) throws IOException {

    ...
    Intent result = new Intent();
    result.putExtra ("address", address);
    this.setResult(Activity.RESULT_OK, result);
    this.finishActivity(RequestCodes.REQUESTCODE_PICK_DESTINATION);
}

最后,当返回MainActivity时:

/** handle the result: */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
    String address = null;
    if(resultCode == Activity.RESULT_OK && result != null) {
        extras = result.getExtras();
        if(extras != null) {
            address = extras.getString("address", null);
            Log.d(LOG_TAG, "the received address is: " + address);
        }
    }
}

SDK文档对其进行了很好的解释:.onActivityResult()