Created
April 13, 2018 11:59
-
-
Save erfanegtfi/986fc013b4d8c4a4f582799bc72e3bb3 to your computer and use it in GitHub Desktop.
Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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