9

I have and app with more than 10 activities and I would like override the method onResume() with the same code in all of them.

I know that I can override this method in each activity, but I'm looking for an efficient solution.

Moreover I would like inside this onResume show a message depending what activity is coming from, for example: if you are in the MainActivity I want that this common onResume detects that is coming from this activity and show I'm coming from MainActivity

Thank you.

user1677266
  • 103
  • 1
  • 5

2 Answers2

18

you should Override activity in a BaseClass and use your BaseClass instead of Activity in the other activities:

public class BaseClass extends Activity 
{

  public void onResume() 
  {
    // common code
  } 
}

public class OtherClass extends BaseClass 
{
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • But, if we extend BaseClass, we cannot extend any other class, so can my solution be the better one? – Shrikant Ballal Sep 27 '12 at 08:38
  • Why you need to extend other classes? previously you are extending only from activity na?? – Eldhose M Babu Sep 27 '12 at 08:58
  • @EldhoseMBabu it is OtherClass to extend BaseClasas – Blackbelt Sep 27 '12 at 09:00
  • I dont know whether what i understand is wrong...BaseClass is inherited from activity and your other activity classes are inherited from BaseClass.. so it is internally got inherited from activity. Then why you need to inherit further? – Eldhose M Babu Sep 27 '12 at 09:13
  • Now I would like inside this onResume show a message depending what activity is coming from, for example: if you are in the MainActivity I want that this common onResume detects that is coming from this activity and show "I'm coming from MainActivity" – user1677266 Sep 27 '12 at 09:48
0

Make a parent activity and override its onResume, then make all your 10 activities extend the creted parent..

Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • Now I would like inside this onResume show a message depending what activity is coming from, for example: if you are in the MainActivity I want that this common onResume detects that is coming from this activity and show "I'm coming from MainActivity" – user1677266 Sep 27 '12 at 09:48