1

I have QR scanner app. There are 3 activities in the app.

1) Main activity - Button to open camera and start scanning

2) QR activity - Scan a QR code

3) Web Activity - On successful scanning, open a web page in the app

Here, the Main activity and QR activity should only launch once, only after the initial install. I read somewhere about using shared preferences. But I am a little confused as to where do I check the variable, as in which activity. Should I check my shared variable in the Main Activity?

This is my first app. Sorry if this is a silly doubt.

SMG
  • 95
  • 1
  • 2
  • 11

4 Answers4

2

It's correct, you have to do it with SharedPreferences.

Here is a good explaination about how to use them

On the first activity shown, you have to add in the onCreate method those lines:

//this retrieve the sharedpreference element
SharedPreference myPref = this.getSharedPreferences(
  "prefName", Context.MODE_PRIVATE);
//this retrieve the boolean "firstRun", if it doesn't exists, it places "true"
var firstLaunch = myPref.getBoolean("firstLaunch", true);
//so, if it's not the first run do stuffs
if(!firstLaunch){
  //start the next activity
  finish();
}      
//else, if it's the first run, add the sharedPref
myPref.edit().putBoolean("firstLaunch", false).commit();

hope this helps

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
1

To complete @Pier Giorgio Misley answer you can put the "firstLaunch" check on your Main Activity or alternatively put it in another "splash" activity

For putting it in the main activity simply set the ui to some neutral color until you decide if you should finish the activity and launch the Web Activity or show the Main Activity logic

Alternatively, you can create a "splash" screen which can function as a bridge activity (which shows some logo or a nice background color) which check the varible and decide which activity to open Android splash

Michael A
  • 5,770
  • 16
  • 75
  • 127
  • Hey, I edited my answer with what I have done with shared preferences. But my app is crashing – SMG Dec 15 '16 at 21:30
  • @SMG your sharedPrefrences check looks good buy why do you perform the check inside a handler? do in on the activity onCreate, regarding the error show the logcat so we can help – Michael A Dec 16 '16 at 07:42
  • Hey, thank you for all your help. I edited my answer with the working app.. – SMG Dec 16 '16 at 14:37
0

As pier mentioned, saving that it has been seen once is the way to go. However, I have found on some older devices, shared preferences is not reliable!

I recommend instead using SQLite database.

Create a table as follows

TABLE NAME: SEEN_ACTIVITY
Column 1: ID INT PRIMARY KEY
Column 2: SEEN VARCHAR

Then, once the activity has been launched, check if there is a record for id = '0' in SEEN_ACTIVITY. If not, then insert one as follows, (0, true).

Then, every time the app launches, check to see if the record exists of (0, true). If not, launch the extra activity.

0
My MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            SharedPreferences settings = getSharedPreferences("prefName",MODE_PRIVATE);
            boolean firstLaunch = settings.getBoolean("firstLaunch", true);
            if(firstLaunch == false) {
                SharedPreferences.Editor editor=settings.edit();
                editor.putBoolean("firstRun",true);
                editor.commit();
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
            else {
                Intent i = new Intent(SplashActivity.this, ScannerActivity.class);
                startActivity(i);
                finish();
            }
        }
    }, SPLASH_TIME_OUT);
}

Wrote this code in a new Splash Activity

SMG
  • 95
  • 1
  • 2
  • 11