Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have an android activity run only on the first time an application is opened?

OK, so I'm playing around with an android app.

The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen.

The first time a user loads the app however, some configuration is required before their list screen is of any value to them.

So my question, is how I can go about displaying the configuration activity the first time the app is opened up, and then the list screen for future openings.

I also want to put a demo button on the configuration screen, so I suppose more than just detecting that it's the first time, I specifically want to detect whether the user has performed certain configurations within the first screen.

like image 702
The Trav Avatar asked Jun 09 '10 22:06

The Trav


People also ask

How do you launch an activity only once for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

Can an activity start itself Android?

Yes it is. If your requirement are like that then there is no harm in doing that.

Is the first method called for an activity in an Android application?

The onCreate() callback is compulsory in all Android applications. It is the first method called when we launch an activity from the home screen or intent. In other words, it is a default callback that is automatically created when you create a new activity.


3 Answers

After the first time the user has loaded the app you could store the details of whether user has performed the configurations in SharedPreferences.

 protected void storeSharedPrefs(String value) {
        /*
         * Storing in Shared Preferences
         */
        editor.putString("first", value);
        editor.commit();  //Commiting changes
    } 

Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences

private boolean first_time_check() {
        /* 
         * Checking Shared Preferences if the user had pressed 
         * the remember me button last time he logged in
         * */
        String first = uPreferences.getString("first", null);
        if((first == null)){
            return false;
        }
        else 
            return true;
    }
like image 114
Primal Pappachan Avatar answered Oct 19 '22 11:10

Primal Pappachan


i like dweebsonduty's method. a similar way to do this is to save their configuration information in files on the SD card. your app could start out by checking for those files and loading the information. if the load is successful, it moves on to the next activity, if not it starts the appropriate activity to prompt the user for input.

I have done this same thing, but instead of swiching activities i just switch views until i have all the info i need, and then move on.

like image 28
mtmurdock Avatar answered Oct 19 '22 10:10

mtmurdock


Many applications actually store the current version in SharedPreferences, and check against it for if an update has been installed. Its a clever way of achieving a "what's changed" popup, or making sure that some settings get set (I would be wary of just having a boolean flag because if you ever want to add an additional setting, you will need a second flag and it gets messy after the third, etc.).

like image 2
Guzba Avatar answered Oct 19 '22 10:10

Guzba