在我的Android应用中,我有两个活动:

1)MapsActivity
2)其他活动

MapsActivity有导航抽屉,通过它打开了OtherActivity。现在,我想从MapsActivity调用一个方法(如updatemap())。

在Google上搜索了很多之后,我在stackoverflow上发现了这个问题
How to call method in main activity from other activity?但这不能解决我的问题。

更新:

这是OtherActivity的代码:

public class OtherActivity extends AppCompatActivity {
   @Override
   protected void attachBaseContext(Context newBase) {.....}

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     .... }

   @Override
   protected void onStart() {
     super.onStart();
     removeConnection(userID)
     .... }

   private void removeConnection(String currentUserId) {
     ...
     // Here I want to call a function from MapsActivity i.e. disaplayLocation()
   }

   public static class ConnectedViewHolder extends
   RecyclerView.ViewHolder{ ... }
}


MapsActivity的代码是:

  public class MapsActivity extends AppCompatActivity implements
  OnMapReadyCallback,
  GoogleApiClient.ConnectionCallbacks,
  GoogleApiClient.OnConnectionFailedListener,
  LocationListener {

      @Override
      protected void attachBaseContext(Context newBase) { ... }

      @Override
      protected void onCreate(Bundle savedInstanceState) { ... }

      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull
       String[] permissions, @NonNull int[] grantResults) { ... }

      private void setUpLocation() { ... }

      public void displayLocation() { ... }  // This is the function i want to call

      private void createLocationRequest() { ... }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... }

      protected synchronized void buildGoogleApiClient() { ... }

      private boolean checkPlayServices() { ... }

      private void requestRuntimePermission() { ... }

      private void startLocationUpdates()  { ... }

      @Override
      public void onMapReady(GoogleMap googleMap) { ... }

      @Override
      public void onConnected(@Nullable Bundle bundle) { ... }

      @Override
      public void onConnectionSuspended(int i) { ... }

      @Override
      public void onConnectionFailed(@NonNull ConnectionResult
       connectionResult) { ... }

      @Override
      public void onLocationChanged(Location location) { ... }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) { ... }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) { ... }

      private void makeConnectionToGetLocation() { ... }

      private void updateConnectedLatLng() { ... }

  }


我仅粘贴代码结构,因为它是1000行代码,此处粘贴没有用,阅读起来也更加复杂。

我试图在displayLocation方法之前添加static关键字,但它在函数内的变量中产生错误,并且不可能将每个变量都设置为global。

最佳答案

如果MapsActivity期望来自OtherActivity的结果,那么最好使用Getting Result From an Activity。只需通过OtherActivity(而不是startActivityForResult())调用startActivity()

static final int MAP_REQUEST = 1;  // your request code
...
private void callOtherActivity() {
    Intent intent = new Intent(this, OtherActivity.class)
    startActivityForResult(intent, MAP_REQUEST);
}


然后在您的MapsActivity中,执行以下操作:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MAP_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            updateMap();
        }
    }
}

09-12 04:43