Skip to content

Instantly share code, notes, and snippets.

@erfanegtfi
Created April 13, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfanegtfi/986fc013b4d8c4a4f582799bc72e3bb3 to your computer and use it in GitHub Desktop.
Save erfanegtfi/986fc013b4d8c4a4f582799bc72e3bb3 to your computer and use it in GitHub Desktop.
Application
public class MyApp extends Application {
public static boolean isAppInBackground = false;
private Activity mCurrentActivity;
public void onCreate() {
super.onCreate();
setScreenOrientation();
this.mMyApp = (MyApp) getApplicationContext();
}
public Activity getCurrentActivity() {
return this.mCurrentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity) {
this.mCurrentActivity = mCurrentActivity;
}
public void onTerminate() {
super.onTerminate();
}
public void setScreenOrientation() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
activity.setRequestedOrientation(1);
activity.setTitle(MyApp.this.getResources().getString(R.string.app_name));
MyApp.this.mMyApp.setCurrentActivity(activity);
activity.getWindow().setSoftInputMode(3);
}
public void onActivityStarted(Activity activity) {
MyApp.this.mMyApp.setCurrentActivity(activity);
}
public void onActivityResumed(Activity activity) {
MyApp.this.mMyApp.setCurrentActivity(activity);
MyApp.isAppInBackground = false;
}
public void onActivityPaused(Activity activity) {
MyApp.isAppInBackground = true;
MyApp.this.clearReferences();
}
public void onActivityStopped(Activity activity) {
}
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
public void onActivityDestroyed(Activity activity) {
MyApp.this.clearReferences();
}
});
}
private void clearReferences() {
if (equals(this.mMyApp.getCurrentActivity())) {
this.mMyApp.setCurrentActivity(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment