Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute something just once per application start?

I'd like to implement an update checker in an application, and I obviously only need this to show up once when you start the application. If I do the call in the onCreate() or onStart() method, it'll be shown every time the activity is created and this is not a viable solution.

So my question is: Is there a way to do something, like check for updates, just once per application start / launch?

I'm sorry if it's a bit hard to understand, I'm having difficulties explaning myself on this one.

like image 380
Michell Bak Avatar asked Sep 09 '11 11:09

Michell Bak


People also ask

How do I run a specific activity?

Go to "Edit Configurations..." in the "Run" menu. In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown. Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.

How do you call a method only once on Android?

You can do this, by using shared preferences . Store a value in shared preferences: SharedPreferences prefs = getPreferences(MODE_PRIVATE); SharedPreferences. Editor editor = prefs.


2 Answers

SharedPreferences seems like ugly solution to me. It's much more neat when you use application constructor for such purposes.

All you need is to use your own Application class, not default one.

public class MyApp extends Application {      public MyApp() {         // this method fires only once per application start.          // getApplicationContext returns null here          Log.i("main", "Constructor fired");     }      @Override     public void onCreate() {         super.onCreate();              // this method fires once as well as constructor          // but also application has context here          Log.i("main", "onCreate fired");      } } 

Then you should register this class as your application class inside AndroidManifest.xml

<application android:label="@string/app_name" android:name=".MyApp"> <------- here     <activity android:name="MyActivity"               android:label="@string/app_name">         <intent-filter>             <action android:name="android.intent.action.MAIN"/>             <category android:name="android.intent.category.LAUNCHER"/>         </intent-filter>     </activity> </application> 

You even can press Back button, so application go to background, and will not waste your processor resources, only memory resource, and then you can launch it again and constructor still not fire since application was not finished yet.

You can clear memory in Task Manager, so all applications will be closed and then relaunch your application to make sure that your initialization code fire again.

like image 195
Vitalii Korsakov Avatar answered Oct 11 '22 11:10

Vitalii Korsakov


looks like you might have to do something like this

PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);        int currentVersion = info.versionCode;       this.versionName = info.versionName;       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);       int lastVersion = prefs.getInt("version_code", 0);       if (currentVersion > lastVersion) {         prefs.edit().putInt("version_code", currentVersion).commit();        //  do the activity that u would like to do once here.    } 

You can do this every time, to check if the app has been upgraded, so it runs only once for app upgrade

like image 25
Yashwanth Kumar Avatar answered Oct 11 '22 11:10

Yashwanth Kumar