user1420042
user1420042

Reputation: 1709

Can I store some methods in a class and use them in another activity?

I have an Android app which uses a lot of activities. All those activities contain methods that are the same in every activity. Can I put those methods into another class (one which just contains those methods) and access them whenever needed in my activities??

I thought about putting the methods in a class and then import that class in my activities. Would that work? Would I be able to use those methods?

Upvotes: 0

Views: 56

Answers (1)

Jan Koester
Jan Koester

Reputation: 1218

Yes, you can create a helper class. This can take methods you can call from every point of your program. Often this classes takes static methods like:

public class Helper {
   public static int callMeFromTheActivities() {
     //do some work
   }
}

so you can call it from your activities like:

Helper.callMeFromTheActivities();

Upvotes: 2

Related Questions