Onboarding Screen in Android studio

Issue

I am trying to built an OnboardingActivity and I have implemented it also but now I want that it should be visible to new user or once per user, I tried shared preference but there was no effect.
I am using Splash Screen as Launcher Activity and MainActivty & onboardingActivity as a next intent.

SplashActivity

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;

public class SplashActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    getSupportActionBar().hide();


    Thread myThread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(3000);
                Intent intent = new Intent(getApplicationContext(), OnBoardActivity.class);
                startActivity(intent);
                finish();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();
}
}

OnBoardActivity

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;


import com.hololo.tutorial.library.Step;
import com.hololo.tutorial.library.TutorialActivity;


public class OnBoardActivity  extends TutorialActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 SharedPreferences preferences =  getSharedPreferences("my_preferences", 
 MODE_PRIVATE);

 // Check if onboarding_complete is false 
 if(!preferences.getBoolean("onboarding_complete",false)) {
// Start the onboarding Activity
Intent onboarding = new Intent(this, OnBoardActivity.class);
startActivity(onboarding);

// Close the main Activity
finish();
return;
}

//slide1
    addFragment(new Step.Builder().setTitle("WELCOME.NAMASTE!!")
            .setSummary("It seems like you are a New User!!\nWe welcome you 
to GFeed.")
            .setBackgroundColor(Color.parseColor("#FFA600")) // int 
background color
            .setDrawable(R.drawable.gh) // int top drawable

            .build());
    //slide2
    addFragment(new Step.Builder().setTitle("CAREER ORIENTED")
            .setSummary("This app includes some awesome career oriented 
    elements such as Internship,different courses related to CS/IT,career 
   tips by experts.")
            .setBackgroundColor(Color.parseColor("#FFA600")) // int 
   background color
            .setDrawable(R.drawable.human) // int top drawable

            .build());
    //slide3
    addFragment(new Step.Builder().setTitle("COLLEGE ORIENTED")
            .setSummary("This app includes some awesome college oriented 
    elements such as college club news,aktu feed,H.O.D'corner and Gsim")
            .setBackgroundColor(Color.parseColor("#FFA600")) // int 
    background color
            .setDrawable(R.drawable.college) // int top drawable

            .build());
   }

  @Override
 public void finishTutorial() {

  SharedPreferences preferences =
        getSharedPreferences("my_preferences", MODE_PRIVATE);

 // Set onboarding_complete to true
 preferences.edit()
        .putBoolean("onboarding_complete",true).apply();

 // Launch the main Activity, called MainActivity
 Intent main = new Intent(this, MainActivity.class);
 startActivity(main);

 // Close the OnboardingActivity
 finish();
   }
 }

Manifest file

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/vtry"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".developer"
        android:screenOrientation="portrait">
    </activity>
    <activity android:name=".notify"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="NOTIFY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".download"
        android:screenOrientation="portrait">
    </activity>
    <activity android:name=".webvr"
        android:screenOrientation="portrait">
        />
        <intent-filter>
            <action android:name="WEBVR" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".gsim"
        android:screenOrientation="portrait">
        />
    </activity>
    <activity android:name=".Internship"
        android:screenOrientation="portrait">
       />
    </activity>
    <activity android:name=".Courses"
        android:screenOrientation="portrait">
       />
    </activity>
    <activity android:name=".Aktu"
        android:screenOrientation="portrait">
        />
    </activity>
    <activity android:name=".Interview"
        android:screenOrientation="portrait">
    </activity>
    <activity android:name=".Reachus"
        android:screenOrientation="portrait">
    </activity>
    <activity android:name=".OnBoardActivity"
        android:screenOrientation="portrait">

    </activity>
    <service
        android:name=".FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>


        </intent-filter>
    </service>
    <!-- Set custom default icon. This is used when no icon is set for incoming notification messages.

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/vtry" />
    <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

</application>

Solution

In the OnboardActivity you are checking if the onboarding is not complete to start the same activity.
The logic should be the inverse, check if onboarding is complete and start MainActivity:

if(preferences.getBoolean("onboarding_complete",false)) {
    // Start Main Activity
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);

    // Close Onboarding
    finish();
    return;
}

So if onboarding is not complete you continue in the same activity.
Also check if the method finishTutorial is being called.

Answered By – Juan Cruz Soler

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published