Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an activity appear only once, when the app is started

I have the following class, SplashActivity.java:

public class SplashScreen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally{
                    Intent tutorial = new Intent(SplashScreen.this, TutorialOne.class);
                    startActivity(tutorial);
                }

            }
        };
        timer.start();
          }
}

I want this activity to load only once, when the app is first installed on the mobile device for the first time. Being new to android I have very little idea about this. I read in places that the SharedPreferences is to be used, but did not understand the implementation. And the thing about this activity is that, the activity has to act as a Launcher when used for the first time, that's what really confused me. Because in the manifest file I am declaring another activity which in my case would be the MainPage.java. So how can I implement this logic ?? Do I call upon the SplashActivity in the MainPage or is there something else that must be done ?? Please help someone ?

Can someone please write down the code to implement this logic if possible?

like image 873
Rakeeb Rajbhandari Avatar asked May 07 '13 12:05

Rakeeb Rajbhandari


People also ask

How do I start an activity only once?

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.

How do I change my app default start activity?

In Android, you can configure the starting activity (default activity) of your application via following “intent-filter” in “AndroidManifest. xml“. See following code snippet to configure a activity class “logoActivity” as the default activity.

How do I start the same activity again on Android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

What is activity launch mode in Android?

This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched. Numerous instances of the activity can be generated, and multiple instances of the activity can be assigned to the same or separate tasks.


1 Answers

Add this code to your onCreate method

    SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    if(pref.getBoolean("activity_executed", false)){
        Intent intent = new Intent(this, TutorialOne.class);
        startActivity(intent);
        finish();
    } else {
        Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();
    }

SharedPreferences will be keep every time you execute the app unless you clean the data from Settings on your Android. The first time is going to get the value from a boolean (activity_executed) saved on such preferences (ActivityPREF).

If it does not find any value it will return false, so we have to edit the preference and set the value to true. The next execution will launch the activity TutorialOne.

finish() erases the current activity from the stack history, so no come back is possible using button back from TutorialOne.

About your manifest, you can set this actitiy with

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> 

Every time the app is executed will launch this activity, but due to the true setted on the "activity_executed" is going to start a new activity with startActivity.

like image 172
AlexBcn Avatar answered Nov 15 '22 06:11

AlexBcn