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



我最近开始了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);
}

您需要在MainActivity:中实现方法onSaveInstanceStateonRestoreInstanceState

@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的.onActivityResult(。

放在一起可能看起来像这个。。。

/** 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文档很好地解释了这一点:了解活动生命周期。

相关内容

最新更新