简体   繁体   中英

Calling a method instead of activity in onActivityResult

I am trying to implement Places.autocomplete using this developer's guide and help here. It is working fine, but, as normal, it is recalling the MainActivity. Hence, it is returning lastknowonlocation already in the MainActivity . I agree, this is a bad design, but an artifact of my zero experience with java.

Now, my question is, is it possible to call a method instead of onActivityResult , ie 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() . 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).

I don't judge, however please understand the basic first.

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