[英]Calling a method instead of activity in onActivityResult
I am trying to implement Places.autocomplete
using this developer's guide and help here.我正在尝试使用此开发人员指南和帮助来实现
Places.autocomplete
。 It is working fine, but, as normal, it is recalling the MainActivity.它工作正常,但像往常一样,它正在调用 MainActivity。 Hence, it is returning
lastknowonlocation
already in the MainActivity
.因此,它已经在
MainActivity
返回lastknowonlocation
。 I agree, this is a bad design, but an artifact of my zero experience with java.我同意,这是一个糟糕的设计,但却是我对 java 的零经验的产物。
Now, my question is, is it possible to call a method instead of onActivityResult
, ie setupViewPager
?现在,我的问题是,是否可以调用一个方法而不是
onActivityResult
,即setupViewPager
?
public void onSearchCalled() {
// Set the fields to specify which types of place data to return.
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields) //no .setCountry...Search whole world
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
// Log.i("LocationSearch", "Place: " + place.getName() + ", " + place.getId() + ", " + place.getAddress());
Toast.makeText(MainActivity.this, "ID: " + place.getId() + "address:" + place.getAddress() + "Name:" + place.getName() + " latlong: " + place.getLatLng(), Toast.LENGTH_LONG).show();
// String address = place.getAddress();
if (place.getLatLng() !=null) {
Lat = place.getLatLng().latitude;
Long = place.getLatLng().longitude;
// setupViewPager();
} else {
Toast.makeText(this, getString(R.string.location_not_found), Toast.LENGTH_LONG).show();
}
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(MainActivity.this, "Error: " + status.getStatusMessage(), Toast.LENGTH_LONG).show();
// Log.i("LocationSearch", status.getStatusMessage());
}
}
}
first of all, onActivityResult()
in itself is pointless without startActivityForResult()
.首先,如果没有
startActivityForResult()
, onActivityResult()
本身就毫无意义。 Now, since autocomplete is handled in another activity (hence the startActivityForResult()
), you're required to have onActivityResult()
in order to read the result of said activity (in this case, the autocomplete value).现在,由于自动完成是在另一个活动中处理的(因此是
startActivityForResult()
),您需要有onActivityResult()
才能读取所述活动的结果(在本例中为自动完成值)。
I don't judge, however please understand the basic first.我不评判,但请先了解基本知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.