如何从其他活动android调用函数

如何从其他活动android调用函数

本文介绍了如何从其他活动android调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Android app I have two activities :

1)MapsActivity
2)OtherActivity

1) MapsActivity
2) OtherActivity

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

MapsActivity has navigation drawer through which I opened OtherActivity . Now , I want to call a method (like updatemap() ) from MapsActivity .

在Google上搜索了很多之后,我在stackoverflow上发现了这个问题如何从其他活动中调用主要活动中的方法?/a>但这不能解决我的问题.

After searching a lot on google I found this question on stackoverflowHow to call method in main activity from other activity? but that doesn't solves my problem .

更新:

这是OtherActivity的代码:

Here is the code for 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的代码为:

Code for MapsActivity is :

  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行代码,此处粘贴没有用,而且阅读起来更加复杂.

I am pasting only structure of code because it is 1000 line of code and pasting here is of no use and become more complicate to read .

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

I tried to add static keyword before displayLocation method but it gives error in variables within function and making every variable to global is not possible .

推荐答案

如果MapsActivity期望来自OtherActivity的结果,那么最好使用从活动中获取结果.只需通过startActivityForResult()(而不是startActivity())调用OtherActivity:

If MapsActivity is expecting a result from OtherActivity, you're better off with Getting Result From an Activity. Simply call OtherActivity via startActivityForResult() (instead of 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中,执行以下操作:

Then in your MapsActivity, do this:

@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();
        }
    }
}

这篇关于如何从其他活动android调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:47