0

Which one is better option for application or activities?

A. BaseActivity class, [OR]

B.Singleton class.

In my application most of activities do same actions like show toasts, maintaining sessions, static variables,show EditText errors, HTTP request/response, etc., For now I'm extends BaseActivity in all my Activities where needed.

Robert
  • 5,191
  • 43
  • 59
  • 113
Sathiamour
  • 127
  • 11

2 Answers2

0

You're on the right path, a BaseClass is preferred because most of those thing you mentioned (Manipulating Toasts, EditText functions) require a Context to work with. A Singleton class would need that Context passed to it with every method call - a BaseClass does not (since it is technically a Context itself).

Hadi Satrio
  • 4,102
  • 2
  • 21
  • 41
0

These are two fundermental different design patterns.

  1. BaseActivity --> Inheritance
  2. Use a Singleton in all activities --> Aggregate pattern

You find many posts in the internet about advantage and disadvantage of both patterns.

For your specific problem I would suggest to use inheritance. It is a common way to do and you should avoid to use the singleton pattern if possible.

Community
  • 1
  • 1
code monkey
  • 1,914
  • 3
  • 19
  • 26