skip to Main Content

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

3

Answers


  1. 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();
        }
    }
    
    Login or Signup to reply.
  2. Add the static keyword to your shared methods

    public static void addNumbers()
    

    Then use:

    FirstActivity.addNumbers();
    

    For more details check out this answer

    Login or Signup to reply.
  3. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search