0

I am implementing an app which have MainActivity , onlyFirstRunActivity and alwaysRunActivity . Now i want that on app installtion or on updation of app my app cycle should be like that:

MainActivity -> onlyFirstRunActivity -> alwaysRunActivity

and after installing or on updation my app cycle should be :

MainActivity -> alwaysRunActivity

How can i implement this situation

User42590
  • 2,473
  • 12
  • 44
  • 85
  • 2
    SharedPreferences could store a boolean `bFirstRun` that tells you whether or not this is the first run. – David M Feb 08 '13 at 16:50
  • but last stored sharedprefences remain on updation.Means how can i configure that the app is updated on updation ?? – User42590 Feb 08 '13 at 16:57
  • 1
    not sure what you mean, but if there is no SharedPreference named `bFirstRun`...then you are in your first run. THEN set it to false (and commit the change). next time thru, it's there and FALSE so goto `alwaysRunActivity` – David M Feb 08 '13 at 17:00
  • if i delete the app and then re install it then what would be value in bFirstRun?? – User42590 Feb 08 '13 at 17:04
  • ah! now i see what you meant. i suppose the SharedPreferences would have been deleted! hmmmm. – David M Feb 08 '13 at 17:05

3 Answers3

1

You can get the update time for your app and save it in a SharedPreference.

Also, you should just make your main activity the always run one.

//In your onCreate for your main activity/.
if(last_update_preference < current_update_time){
   Update last update preference to current update time.
   Run first activity. (Which will finish and bounce you back);
}

To get the current_update_time:

How to get app install time from android

For last update time, see:

http://developer.android.com/guide/topics/data/data-storage.html#pref

Community
  • 1
  • 1
Edison
  • 5,961
  • 4
  • 23
  • 39
  • i think it will only work for updation . What about on first install?? And is sharedprefence a secured way?? – User42590 Feb 08 '13 at 16:54
  • It will work for first install if you return 0 as default value for SharedPreference, last modified should be install time. (The linked stack overflow question shows it) – Edison Feb 08 '13 at 16:56
  • what will be value of last_update_preference for first time?? – User42590 Feb 08 '13 at 17:08
  • You can set it to -1 or 0. (getInt() in SharedPreference) – Edison Feb 08 '13 at 17:10
  • if time is stored in last_update_preference and current_update_time. for first time it can be true . and for second it also true because last update time was current update time. so in this case last update time is less than current update time – User42590 Feb 08 '13 at 17:17
  • You will have to update it to the current update time. I edited the answer to reflect the change. – Edison Feb 08 '13 at 17:19
  • i am also saying that... but i am confuse that last_update_time will always less than the current_update_time – User42590 Feb 08 '13 at 17:39
0

You can try something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    App app = (App)getApplication();
    if (app.getApi() != null){
        super.onBackPressed();
        startActivity(new Intent(this, MainActivity.class));
        return;
    }else showLoginForm();//or do something else.
}
Vetalll
  • 3,472
  • 6
  • 24
  • 34
  • i did not understand why you get app.getAp?? and what is login form?? – User42590 Feb 08 '13 at 17:35
  • the method getApi() return object of api class, which creates from shared preferences. So it mean that the start is not first. And i starts new activity. Or if api = null ots mean that the start is first, and i show login form. – Vetalll Feb 09 '13 at 11:26
0

Like this you can make a difference between a new install and a update.

    String StoredVersionname = "";
    String VersionName;
    AlertDialog LoginDialog;
    AlertDialog UpdateDialog;
    AlertDialog FirstRunDialog;
    SharedPreferences prefs;

    public static String getVersionName(Context context, Class cls) {
                try {
                    ComponentName comp = new ComponentName(context, cls);
                    PackageInfo pinfo = context.getPackageManager().getPackageInfo(
                            comp.getPackageName(), 0);
                    return "Version: " + pinfo.versionName;
                } catch (android.content.pm.PackageManager.NameNotFoundException e) {
                    return null;
                }
            }

    public void CheckFirstRun() {
                VersionName = MyActivity.getVersionName(this, MyActivity.class);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
                StoredVersionname = (prefs.getString("versionname", null));
                if (StoredVersionname == null || StoredVersionname.length() == 0){
                    FirstRunDialog = new FirstRunDialog(this);
                    FirstRunDialog.show();
                }else if (StoredVersionname != VersionName) {
                    UpdateDialog = new UpdateDialog(this);
                    UpdateDialog.show();
                }
                prefs.edit().putString("versionname", VersionName).commit();
            }
Mark
  • 156
  • 1
  • 7