Take action only when the app starts for the first time after installation

3

I'm making an android app that needs to show a message when it's first started. For example: the user installs my app and runs. In this run, the app displays a message for the app to get a license and the user clicks "ok" to accept the terms and release the app.

From there, the next time it runs the application, this message should not be displayed again.

I tried to make a counter in OnCreate but it did not. Is there anything I can do?

My logcat after entering the user code @array :


02-26 16:42:40.895: D/AndroidRuntime(818): Shutting down VM
02-26 16:42:40.895: W/dalvikvm(818): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-26 16:42:40.935: E/AndroidRuntime(818): FATAL EXCEPTION: main
02-26 16:42:40.935: E/AndroidRuntime(818): java.lang.RuntimeException: Unable to resume activity {com.example.testegerador/com.example.testegerador.MainActivity}: android.app.SuperNotCalledException: Activity {com.example.testegerador/com.example.testegerador.MainActivity} did not call through to super.onResume()
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.os.Looper.loop(Looper.java:137)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.main(ActivityThread.java:5041)
02-26 16:42:40.935: E/AndroidRuntime(818):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 16:42:40.935: E/AndroidRuntime(818):  at java.lang.reflect.Method.invoke(Method.java:511)
02-26 16:42:40.935: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-26 16:42:40.935: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-26 16:42:40.935: E/AndroidRuntime(818):  at dalvik.system.NativeStart.main(Native Method)
02-26 16:42:40.935: E/AndroidRuntime(818): Caused by: android.app.SuperNotCalledException: Activity {com.example.testegerador/com.example.testegerador.MainActivity} did not call through to super.onResume()
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.Activity.performResume(Activity.java:5184)
02-26 16:42:40.935: E/AndroidRuntime(818):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
02-26 16:42:40.935: E/AndroidRuntime(818):  ... 12 more
02-26 16:44:05.165: I/Process(818): Sending signal. PID: 818 SIG: 9
    
asked by anonymous 18.02.2015 / 16:15

1 answer

5

You have two options: Verify that the application has been licensed and not show the message, or use SharedPreferences .

Option 1 : Let's say you are using a database to enter the license for the user. This would make it easier to control access. You can insert a column in the database (eg: Column: hasLicense) and then take the value of that column and do an action.

SharedPreferences - You can read more about documentation .

With this method, you will store persistent data in the device memory. The handling of it is quite simple, see the code:

public class MyClass extends Activity {
    SharedPreferences sPreferences = null;

    @Override
    public void onCreate (Bundle cicle) {
        super.onCreate(cicle);
        setContentView(R.layout.SEU_LAYOUT.xml); // Coloque seu Layout aqui!

        // SharedPreferences
        sPreferences = getSharedPreferences("firstRun", MODE_PRIVATE);
    }

    @Override
    public void onResume () {
        super.onResume();

        if (sPreferences.getBoolean("firstRun", true)) {
            sPreferences.edit().putBoolean("firstRun", false).apply();
            Toast.makeText(getApplicationContext(), "primeiro launcher", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "segundo? terceiro?...", Toast.LENGTH_LONG).show();
        }
    }
}

Remembering that in the if (sPreferences.getBoolean("firstRun", true)) condition, it checks to see if it is the first run of the application. If yes, it will execute the code with the toast stating that it is the first execution.

  • The code was in error, but has been cleaned and tested.
18.02.2015 / 16:49