简体   繁体   English

在 onActivityResult 中调用方法而不是活动

[英]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.

相关问题 onActivityResult()不是从Activity调用 - onActivityResult() is not calling from Activity 在调用活动中未调用onActivityResult - onActivityResult not being called in calling activity 片段onActivityResult方法执行调用活动onActivityResult - Fragment onActivityResult method on executing calls activity onActivityResult Activity 在 onActivityResult 方法上变为 null - Activity becomes null on onActivityResult method 在开始的活动完成之前调用onActivityResult - onActivityResult calling before the started activity finish 向后调用和onActivityResult()不在Fragment中调用,但在活动中调用 - Backward calls and onActivityResult() not calling in Fragment but calls in activity 调用设置活动时过早调用了onActivityResult() - onActivityResult() called prematurely while calling Settings activity 无法将结果返回到调用活动的 onActivityResult() - Unable to get result back to onActivityResult() of calling activity Facebook callbackManager 方法 onActivityResult 未在 Fragment onActivityResult 方法中调用 - Facebook callbackManager method onActivityResult not calling in Fragment onActivityResult method TabActivity的子活动是否支持onActivityResult()方法? - Does child activity of TabActivity supports onActivityResult() method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM