简体   繁体   中英

Call activity A method from Activity B Android

I'm a new to java and android. I was working on my own app but I'm having a problem in passing a method from Activity A to Activity B.

Here is what I did :

ActivityA has Demo() method.

public class ActivityA extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  protected void demo() {
    // Do something    
  }
}

I created the below class to access the method of ActivityA to ActivityB:

public class External {
    private ActivityA activitya;
    private static External instance = null;

    public External(ActivityA activitya) {
        this.activitya = activitya;
    }

    static public External getInstance(ActivityA activitya) {
        if (instance == null) {
            instance = new MyntraExternal(activitya);
            return instance;
        } else {
            return instance;
        }
      }
   }

Now how can I proceed further? I'm having lots of problem in getting the method which is in ActivityA from ActivityB. Please anybody help.

Edit :

ActivityB is my launcher class and I want some access from ActivityA's method in ActivityB. What to do ?

Since you are new to Android, I will tell you it's a bad practice call methods from Activity A to B or vice versa, you can pass parameters from one activity to another using intents and bundles and if you need to pass parameters from the second activity to the first you need to use the override method onActivityResults

Here are some usefull link about passing parameters from one activity to another: https://www.dev2qa.com/passing-data-between-activities-android-tutorial/ In this link you can see a example of how things work.

Hope it helps.

--EDIT (if you need to call a function from B to A in case you want to change something in A upon creation this is the best and simplest way to do it):

In Activity B:

Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("Work","doJump");
startActivity(intent);

In Activity A: onCreate:

String extra = getIntent().getStringExtra("Work");
if(extra != null && extra.equals("doJump")){
  jump();
}

make that method public and static and then access it using class name. eg In your 2nd activity, use ActivityB.demo()

Try using startActivityForResult

To start activity B from activity A

    startActivityForResult(intent, SOME_CODE)

And to be called back on result you will need to add the following code the also in activity A

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(code){
            SOME_CODE -> if (resultCode == Activity.RESULT_OK) doSomething()
        }
    }

To tell Activity A to call the method, in activity B you can say:

        setResult(Activity.RESULT_OK)
        finish()

After B is finished, onActivityResult in A will be executed

To go back to A without executing the "doSomething()" method,

        setResult(Activity.RESULT_CANCELED)
        finish()

Please try this way

public class ActivityA extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void demo() {
    // Do something    
  }
}

public class ActivityB extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityA activityA = new ActivityA();  // create object
    activityA.demo();  // 
  }
}

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