craftdeer
craftdeer

Reputation: 1025

How can you call a method of an activity from another activity?

Here is my first activity

public class FirstActivity extends AppCompatActivity {

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

    public void addNumbers() {
        ////
    }

}


public class SecondActivity extends AppCompatActivity {

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

     // I need to call addNumbers() from here
}

I have tried instantiating FirstActivity in SecondActivity like so but I get an error. And I also heard its not the best practice to instantiate the whole activity.

FirstActivity firstActivity = new FirstActivity();
firstActivity.addNumbers();

Upvotes: 0

Views: 396

Answers (3)

SamanSepahvand
SamanSepahvand

Reputation: 86

It is better to put the mathematical operations in a repository and, if necessary, find the instance of that repository each time by calling its instance. You can use these codes.

First Make this Repository

public class CalculatorRepository {
    private static CalculatorRepository calculatorRepository = null;

    private CalculatorRepository() {

    }

    public static CalculatorRepository getInstance() {
        if (calculatorRepository == null) {
            synchronized (CalculatorRepository.class) {
                if (calculatorRepository == null) {
                    calculatorRepository = new CalculatorRepository();
                }
            }
        }
        return calculatorRepository;
    }

    public int AddFunction(int number1, int number2) {
        try {   // for  better handler (Optional)
            return number1 + number2;
        } catch (Exception e) {
            return 0;
        }
    }

    //  another function
    //  .
    //  .
    //  .
}

Second Call Method

Now you can easily use other methods inside by calling this class

int result = CalculatorRepository.getInstance().AddFunction(nmuber1,nmunber2);

Upvotes: 0

Umar Farooq
Umar Farooq

Reputation: 271

Instead of calling method in second activity, create a separate public class e.g. Functions.java, and implement it as

public class Functions {
    public void addNumbers() {
        ////
    }
}

Now in your second activity where you want to call the method

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        // Calling addNumbers() from here
        new Functions().addNumbers();
    }
}

Upvotes: 4

Ashique Bava
Ashique Bava

Reputation: 2706

Add the static keyword to your shared methods

public static void addNumbers()

Then use:

FirstActivity.addNumbers();

For more details check out this answer

Upvotes: -1

Related Questions