How to launch activity only once when app is opene

2019-01-02 19:58发布

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?

标签: android
8条回答
孤独总比滥情好
2楼-- · 2019-01-02 20:26

This is my code to bring the OnBoarding Activity for the first time. If it's not the first time then go directly to the Home Activity.

    private void checkFirstOpen(){
    Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (!isFirstRun) {
        Intent intent = new Intent(OnBoardingScreen.this, Home.class);
        startActivity(intent);
        finish();

    }

    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", 
    false).apply();
}
查看更多
与风俱净
3楼-- · 2019-01-02 20:29
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();
}
查看更多
步步皆殇っ
4楼-- · 2019-01-02 20:29

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();
    }
查看更多
裙下三千臣
5楼-- · 2019-01-02 20:30

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();
}
查看更多
其实,你不懂
6楼-- · 2019-01-02 20:41

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

查看更多
看风景的人
7楼-- · 2019-01-02 20:42

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.

查看更多
登录 后发表回答