19

I would like to have a quick way to be able to log all the calls to all the super methods called by the activity.

I have tried this https://github.com/stephanenicolas/loglifecycle

but for some reason it does not work with AppCompatActivity...

I could ask my IDE to override all the methods; but how to add Log to all of them? Manually? There must be a way..

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • 1
    dunno if you tried Hugo: https://github.com/jakeWharton/hugo – user2511882 Apr 30 '15 at 17:02
  • with Hugo you have to put @logsomethig in front of each method you want to log, same as doing manually :-((( – Lisa Anne Apr 30 '15 at 17:44
  • @LisaAnne You want to log all methods or just calls to supers? – Sid May 07 '15 at 14:02
  • Unfortunately, I think you are going to have to bite the bullet and add them all manually. You could create a class to extend from that only has logging in it. That way you do not clutter your actual class, and you can reuse it in your other classes. – Knossos May 07 '15 at 14:02
  • @Assa only the supers would suffice – Lisa Anne May 07 '15 at 14:05
  • Over what length of time? You can get the complete list of calls with method tracing (http://developer.android.com/tools/debugging/debugging-tracing.html), but that's only suitable for short periods. – fadden May 07 '15 at 19:59
  • use single base activity /Fragment as parent class and extend all the Activities and fragments with base class u created above..in base class Add Logs in each method , also add calleeClass.getSimpleName() to make sure which class is currrently on Top! – Jivraj S Shekhawat May 11 '15 at 09:39

3 Answers3

12

You could go around and play yourself with stacktrace

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

From the docs:

The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

EDIT: There seems to be a whole post about this HERE

Community
  • 1
  • 1
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • How do you want subscribe for stacktrace of activities. TC want to log each method in activity. – TAC May 13 '15 at 10:49
  • stacktrace goes beyond activity only methods, it logs everything. You need to filter out the results if you want only activity methods, see the link I posted, it is very detailed. – Bojan Kseneman May 13 '15 at 11:27
4

It is time to look for AspectJ http://code.google.com/p/android-aspectj/ and this is perfect article http://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/

you just need to setup aspect execution

aspect Trace{
    pointcut traceMethods() : (execution(* com.xyz.*.*(..)));

    before(): traceMethods(){
        Signature sig = thisJoinPointStaticPart.getSignature();
        String sourceName =  thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName();
        Log.d("AOPLogging: " + sourceName,  "Some method has called")
   }
}

about expressions you could read on AOP

TAC
  • 341
  • 1
  • 5
  • So this is basically a library that uses stacktrace? – Bojan Kseneman May 13 '15 at 11:32
  • No, it is big framework Aspect Oriented Programing. it has few stages pre-compile and run-time. So generally it is EE framework. but it is works perfect on android. So it is big tool for just logging porpoises. But TC task is regular issue for AOP. – TAC May 13 '15 at 13:30
4

EDITED: If you can afford going only API 14+ you can use

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

on Application (https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks))

It doesn't provide all methods, but have the most used ones.

abstract void   onActivityCreated(Activity activity, Bundle savedInstanceState)
abstract void   onActivityDestroyed(Activity activity)
abstract void   onActivityPaused(Activity activity)
abstract void   onActivityResumed(Activity activity)
abstract void   onActivitySaveInstanceState(Activity activity, Bundle outState)
abstract void   onActivityStarted(Activity activity)
abstract void   onActivityStopped(Activity activity)
luciofm
  • 737
  • 5
  • 14