活动应在应用程序的手机第一次运行只运行一次应在、应用程序、手机

2023-09-12 04:43:55 作者:鹄息

我已经创建了一个名为活动activity_create_password这将创建密码的应用程序时,应用程序启动的单元上的第一次,下次开始就应该表现出活动命名activity_insert_password。我怎么能做到这一点我没有收到请帮助。

I have created an activity named activity_create_password which will create password for the app when the application is started on the cell for the first time and next time onwards it should show the activity named activity_insert_password. how can I achieve this I am not getting Please Help.

推荐答案

您必须使用共享preferences 来实现这一目标,您的code应该像

You have to use SharedPreferences to achieve this, your code should be something like

SharedPreferences prefs = mContext.getSharedPreferences("appName", 0);
SharedPreferences.Editor editor = prefs.edit();
Intent intent;
if (prefs.getBoolean("isInitialAppLaunch", false))
{
    intent = new Intent(this, activity_insert_password.class);
    startActivity(intent);
}
else
{
    //First Time App launched, you are putting isInitialAppLaunch to false and calling create password activity.
    editor.putBoolean("isInitialAppLaunch", false);
    intent = new Intent(this, activity_create_password.class);
    startActivity(intent);
}