Do Something When App Starts?
I have a very quick question. Whenever the user downloads my app, for some reason the volume is at zero. I want to raise the volume whenever the user enters the app for the first
Solution 1:
Actually changing volume without any user interaction would be a bad user experience. However for answering this question. You can use SharedPreferences
When your app starts:
Contextcontext= getActivity();
SharedPreferencessharedPref= context.getSharedPreferences("com.example.myapp.PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
intdefaultValue=0;
longopenedState= sharedPref.getInt("isAppOpenedBefore", defaultValue);
if (defaultValue == openedState)
{
// First launch// Change volume// Writing app already opened state
SharedPreferences.Editoreditor= sharedPref.edit();
editor.putInt("isAppOpenedBefore", 1);
editor.commit();
}
Solution 2:
You can take a look at the Activity Lifecycle.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You can use the onStart method in you launcher activity and make sure that everything is not null.
If this should happen only at the first start of the app per device you can set a sharedPreference at the ned of your on start to true and in the beginning of onStart you can check for that value. You can check if SharedPreference has that specific value.
http://developer.android.com/reference/android/content/SharedPreferences.html
Post a Comment for "Do Something When App Starts?"