Is it possible to reuse methods in different activities? Say for example, I have retrieveAllStudents() in StudentActivity. Can I make it static or something and call the method in ClassActivity? Or do I need to duplicate the method in both activities?
Which one is correct?
Example 1:
StudentActivity
public static ArrayList<Student> retrieveAllStudents(){
...
return studentList;
}
ClassActivity
import StudentActivity
ArrayList<Student> studentList= StudentActivity.retrieveAllStudents();
Example 2:
StudentActivity
public static ArrayList<Student> retrieveAllStudents(){
...
return studentList;
}
ClassActivity
public static ArrayList<Student> retrieveAllStudents(){
...
return studentList;
}
ArrayList<Student> studentList= retrieveAllStudents();
If it is
public staticit is definitely accesible from other activities (and any other class in your app). However, when activities call functions on each other, it can lead to overly complex code. Consider moving the getStudent() function and other shared functionality to a separate Student class.Edit yes it is possible to reuse methods in other classes. This is very common and considered a best practice. Given your two examples, the first is more correct.