I have an application in android which has no. of activity. Now I needed that the context of currently running activity in a non-activity class. How can I get that.
You can use Application
class :
public class MyApplication extends Application {
private static Context context;
private static Activity activity;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public synchronized static Context getAppContext() {
return MyApplication.context;
}
/**
* setCurrentActivity(null) in onPause() on each activity
* setCurrentActivity(this) in onResume() on each activity
*
*/
public static void setCurrentActivity(Activity currentActivity) {
activity = currentActivity;
}
public static Activity currentActivity() {
return activity;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With