简体   繁体   中英

Run Activity only for First time after installation of app Android

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

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

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.
}

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();
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM