0

Can some one help me get the reference to the current activity from the Myapp that extends Application. I'm trying to implement a simple shake feature that uses the sesmic library. Once the shake is detected I need to have the reference to the current activity so that I can save a screen shot of the current view. I need to access the current activity something like

enter public class MyApp extends Application {

public onSomeTriggerCallThisFunction() {
    Activity currentActivity = getCurrentActivity();
}}

Can someone help me with getCurrentActivity()

Varun R
  • 45
  • 1
  • 9
  • 6
    Go over this: http://stackoverflow.com/questions/11411395/how-to-get-current-foreground-activity-context-in-android – Geetha Aug 26 '15 at 05:43
  • That question and this answer: http://stackoverflow.com/a/13994622/3162918 – bendaf Aug 26 '15 at 06:16

2 Answers2

2

For API level 14 :

They added new public function

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

Implementing them in your application class can give access to current activity.

To read about it's Documentation...

An Example to get started with...

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
2

you should set currentActivity when you call each activity in onCreate or onResume activity method like that. G is my Application class and currentActivity is my static field in G class

@Override
protected void onResume() {
    super.onResume();
    G.currentActivity = this;
}
Payam Mobarraei
  • 197
  • 1
  • 8