Perform an action only when the app starts for the first time after installation

Asked

Viewed 1,919 times

3

I’m making an android app that needs to show a message when starting for the first time. For example: the user installs my app and runs it. In this run, the app shows a message for the app to get a license and the user clicks "ok" to accept the terms and release the use of the app.

From there, the next time he runs the application, that message should not be displayed again.

I tried to do an accountant on OnCreate but it didn’t. There’s something I can do?


Man logcat after entering 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
  • Wouldn’t it be better to paste the code? Read: http://answall.com/help/mcve

  • @Guilherme, the user inserted the image pointing out the errors that he had found in my code, but that has already been corrected. See that I had declared Sharedpreferences private for onCreate.

  • @Guest the point is not this but rather make something readable and easy "test". :)

  • @William, ah, yes. I’m sorry. I believe the correct one was: either he just edit with the code (as you said), or even send the image in the comments of the reply below (he already commented that there was an error.).

  • @Guest I didn’t really read the comments, but it’s not quite you that would have to edit something and rather it form a question using the link I indicated, your answer looks good, I haven’t paid attention to the details so I’m a little out of it. Just to emphasize, we are a community of q&a, comments are usually for quick discussions, codes and errors relevant to questions should not be posted in the comments, but rather in the scope of the question :) I believe you already know. Good night Guest

1 answer

5

You have two options: check if the application has been licensed and do not show the message, or use Sharedpreferences.

Option 1: let’s say that you are using a database to enter the license for the user. So it would be easier to control access. You can insert a column into the database (e.g: Column: temLicense) and then take the value from that column and do an action.

Sharedpreferences - You can read more about documentation.

With this method, you will store persistent data in the device’s memory. Handling 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 condition if (sPreferences.getBoolean("firstRun", true)), it checks if it is the first execution of the application. If yes, it will execute the code with Toast stating that it is the first execution.

  • The code was wrong, but it’s been fixed and tested.
  • In this part of the code: Sharedpreferences sPreferences = Preferencemanager.getSharedPreferences("file_first_run", MODE_PRIVATE); An error appears in "getSharedPreferences" The error is as follows: "The method getSharedPreferences() in the type Preferencemanager is not applicable for the Arguments (String, int)" And the option you give is to remove getSharedPreferences()

  • Kaamis, I just corrected the code. Redo it with the correction and tell me if it worked. No waiting!

  • Guest still occurs the same error.

  • One more Guest question, this information will be saved even if the application is destroyed and not only hidden?

  • Kaamis, yes the information will be saved even if the application is closed/destroyed. It is private information to the developer. I’ll fix the code, I’ll be right back with it fixed and tested!

  • Kaamis, I just corrected the code, tested it, and it worked! I’m waiting for your answer to confirm if it’s working or not. Hugs!

  • Guest, I don’t know what might be since you tested but this error appears as in the image I put in the question now. I tried to declare Sharedpreference just below in public class Mainactivity as you can see and I took the statement of Oncreate left only the name. It left these errors but when running on the emulator it does not run, in fact it does not open. It appears that error "Ufortunatelly...". Thank you very much for your attention!

  • @kaamis, a thousand apologies! I had the wrong definition of Sharedpreferences, it was supposed to be global, but I ended up declaring it as private. I just fix and again a thousand apologies for the mistake! Remember to modify the line setContentView(R.layout.seu_layout_aqui.xml) for its layout created in layout/.

  • Guest I modified but still gave error "Ufortunatelly..." at the time of running, I do not know what it is but still thank you for your help and attention!

  • I need you to show me yours logcat

  • edited the question and put the logcat.

  • @kaamis, already fixed the code. Can you test it! What was missing is that I was not calling the method super to the onResume, causing the error SuperNotCalled.

  • Thank you very much even array! Now it worked right! I tried to take a look at Logcat but I can’t understand almost anything. Now I took a look and saw the line Caused by and the error explanation just ahead. Correct me if I’m wrong, what comes up would be the error deccorer and on the line caused by the error itself?

  • @kaamis, the line with the caused by is a great resumo of the error, that is, only with this line you will already have a certain awareness of the error, but already in the lines above, the error comes from the beginning, ie, the error comes fully detailed, from start to finish, something like: Where it occurred, what occurred, how it occurred and etc.

  • Look at the first line: Shutting down VM this line already says that the virtual machine has been shut down, and in the following lines it details the error until arriving at the description: Caused by: [erro]

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.