How to launch activity only once when app is opened for first time?

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I have an activity that i only want to run when the application is ran for the first time.

And never again. It is a facebook login activity. I only want to launch it once when the app is initially opened for the first time.

How do i go about doing this?

回答1:

What I've generally done is add a check for a specific shared preference in the Main Activity : if that shared preference is missing then launch the single-run Activity, otherwise continue with the main activity . When you launch the single run Activity create the shared preference so it gets skipped next time.

EDIT : In my onResume for the default Activity I do this:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());     boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);     if(!previouslyStarted) {         SharedPreferences.Editor edit = prefs.edit();         edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);         edit.commit();         showHelp();     }

Basically I load the default shared preferences and look for the previously_started boolean preference. If it hasn't been set I set it and then launch the help file. I use this to automatically show the help the first time the app is installed.



回答2:

something like this might work.

public class MyPreferences {      private static final String MY_PREFERENCES = "my_preferences";        public static boolean isFirst(Context context){         final SharedPreferences reader = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);          final boolean first = reader.getBoolean("is_first", true);         if(first){             final SharedPreferences.Editor editor = reader.edit();             editor.putBoolean("is_first", false);             editor.commit();         }         return first;     }  }

usage

boolean isFirstTime = MyPreferences.isFirst(MyActivity.this);


回答3:

Post the following code within your onCreate statement

   Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)             .getBoolean("isFirstRun", true);      if (isFirstRun) {         //show start activity          startActivity(new Intent(MainActivity.this, FirstLaunch.class));         Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)                 .show();     }          getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()                 .putBoolean("isFirstRun", false).commit();

Replace FirstLaunch.class with the class that you would like to launch



回答4:

SharedPreferences dataSave = getSharedPreferences("firstLog", 0);  if(dataSave.getString("firstTime", "").toString().equals("no")){ // first run is happened } else{ //  this is the first run of application SharedPreferences.Editor editor = dataSave.edit();                 editor.putString("firstTime", "no");                 editor.commit(); }


回答5:

I had done this without Shared Prefrence...as I know shared prefrence consumes some memory so I used public static boolean variable in global class....First I made Global Class Appconfig...and then I made boolean static variable like this :

public class Appconfig {      public static boolean activity = false; }

then I used this public static boolean variable into my welcome Activity class. I am Using License agreement page. which I have to use only at once in my application then never display further whenever i run the application. so i had put condtion in welcome activity...if the welcome class run first time so the static boolean variable is false...

 if (Appconfig.activity == false) { Intent intent = new Intent(); intent.setClass(WelcomeActivity.this,LicesnceActivity.class); startActivity(intent); WelcomeActivity.this.finish(); } if (Appconfig.activity == true) {  Intent intent = new Intent(); intent.setClass(WelcomeActivity.this, MainActivity.class);     startActivity(intent);  }

Now at Licesnce Activity class I made :

Appconfig.activity=true;

So whenever I run the Application the second activity "Main activity" run after Welcome activity not License Activity....



回答6:

Declared in the globally

   public int count=0     int tempInt = 0;

in your onCreate function past this code at first.

   count = readSharedPreferenceInt("cntSP","cntKey");    if(count==0){        Intent intent = new Intent();        intent.setClass(MainActivity.this, TemporaryActivity.class);        startActivity(intent);        count++;        writeSharedPreference(count,"cntSP","cntKey");        }

Past these two method outside of onCreate

    //Read from Shared Preferance     public int readSharedPreferenceInt(String spName,String key){     SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE);     return tempInt = sharedPreferences.getInt(key, 0);     }           //write shared preferences in integer     public void writeSharedPreference(int ammount,String spName,String key ){      SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);     SharedPreferences.Editor editor = sharedPreferences.edit();      editor.putInt(key, ammount);     editor.commit(); }


回答7:

Declare Globally

public int count=0;    int tempInt = 0;

First Screen OnCreate

count = readSharedPreferenceInt("cntSP","cntKey");                 if(count==0){                     Intent i = new Intent(SplashScreen.this, IntroActivity.class);                     startActivity(i);                     count++;                     writeSharedPreference(count,"cntSP","cntKey");                 }                  else {                      Intent i = new Intent(SplashScreen.this, MainActivity.class);                     startActivity(i);                     // close this activity                  }

Now Outside Oncreat Method

public int readSharedPreferenceInt(String spName,String key){         SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);         return tempInt = sharedPreferences.getInt(key, 0);     }      //write shared preferences in integer     public void writeSharedPreference(int ammount,String spName,String key ){          SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);         SharedPreferences.Editor editor = sharedPreferences.edit();          editor.putInt(key, ammount);         editor.commit();     }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!