In order to access Activity
methods without needing to pass context
every time you need it, it's useful to keep a reference to the currentActivity in your application object.-
public class YourApplication extends Application {
public YourApplication() {
instance = this;
}
public static YourApplication getInstance() {
return instance;
}
public Activity getCurrentActivity() {
return currentActivity;
}
public void setCurrentActivity(Activity currentActivity) {
this.currentActivity = currentActivity;
}
}
In your AndroidManifest.xml
<application
android:name=".YourApplication"
...
>
</application>
And in your Activities
onResume
method.-
@Override
protected void onResume() {
super.onResume();
YourApplication.getInstance().setCurrentActivity(this);
}
Also, I usually have a ParentActivity
to be extended by the rest of my activities, grouping some common logic like this one.