0

I am currently developing an application that on the first time the application opens, shows the setup interface and the succeeding times the application is opened, it shows the settings interface. The way I achieved this is that once my application opens, it checks if the shared preferences is null. If the shared preferences is null, it means it was the first time that the application is opened and it will set a value in the shared preferences. Then the next time that the application is opened, when the shared preferences is checked, it isn't null anymore and will go to the settings interface. The problem is, When I run it the first time, it only shows a white screen with no status bar or anything. Below are code snippets of my application

 private void saveSharedPref()
{
    SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_FILE, 0);
    SharedPreferences.Editor editor = preferences.edit();

    //Save values from Edit Text Controls
    editor.putString("firstTime", "1234");
    editor.commit();
}

private void loadSharedPreferences()
{
    SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_FILE, 0);

    //Null check in case file does not exist
    if(preferences != null) {
        String checkFirst = preferences.getString("firstTime", "");
        if (!checkFirst.equalsIgnoreCase("1234"))
        {
            Intent sendToSetup = new Intent (this, Setup.class);
            startActivity(sendToSetup);
        }
        else
        {
            saveSharedPref();
        }
    }
}

I call loadSharedPreferences() in my onCreateMethod

public class Setup extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.setup_pg1);
}

This is where the app gets redirected when the user open it for the first time.

Any help would be greatly appreciated. Thanks in advance! P.S. sorry if my english isnt good

EDIT: here's the androidmanifest

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/icon1"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".SafeSwipe"
        android:label="SafeSwipe-UI v.2"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name=".MainActivity"
        android:label=""
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

setup_pg1

<TextView
    android:text="Setup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="102dp"
    android:id="@+id/textView" />

<Button
    android:text="Continue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginTop="71dp"
    android:id="@+id/button7" />

setup file

public class Setup extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.setup_pg1);
}

}

4 Answers4

1

First, if(preferences != null) will never be true because you'll always get a non-null value from getSharedPreferences()

I call loadSharedPreferences() in my onCreate Method

No you don't... here's your code

public class Setup extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup_pg1);
    }
}

Try again. But use a boolean value rather than some random string.

public class MainActivity extends AppCompatActivity {

    public static final String PREFS_NAME = "MyPrefsFile";
    public static final String KEY_FIRST_TIME = "firstTime";

    @Override
    protected void onCreate(Bundle b){
       super.onCreate(b);
       setContentView(R.layout.activity_main);

       SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
       boolean firstTime = prefs.getBoolean(KEY_FIRST_TIME, false);

       if (!firstTime) {
           SharedPreferences.Editor editor = prefs.edit();
           editor.putBoolean(KEY_FIRST_TIME, true);
           editor.commit();    
       } else {
           Intent sendToSetup = new Intent (this, Setup.class);
           startActivity(sendToSetup);
           finish();
       }
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Your if(preferences != null)is not working here, cause preferences is not null. It's just an empty preference. You can try out the below code:

private void loadSharedPreferences()
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String checkFirst = preferences.getString("firstTime", "");
    if(!checkFirst.equals("")) {        
        Intent sendToSetup = new Intent (this, Setup.class);
        startActivity(sendToSetup);
    }
    else
    {
        saveSharedPref();
    }
}

saveSharedPref() method:

private void saveSharedPref()
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    //Save values from Edit Text Controls
    preferences.edit().putString("firstTime", "1234").apply();

}

Call loadSharedPreferences() in onCreate() of Setup Activity:

Hope this helps.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
0

Try this: Instead of checking preferences != null try this..

if (preferences.getString("firstTime","").equals("1234"))
{
     Intent sendToSetup = new Intent (this, Setup.class);
     startActivity(sendToSetup);
}
else
     saveSharedPref();
0

I don't see any crucial problem in this code. But I have some assumption that you have already called the saveSharedPref() method while testing your program and value of firstTime is already exist. Because SharedPreferences are not deleted after recompiling the program this statement:

if (!checkFirst.equalsIgnoreCase("1234"))

gives false. The best way to check this is to set a breakpoint in loadSharedPreferences() method and verify it. If it is so try to remove sharedPreferences (see the answer https://stackoverflow.com/a/3687333/7677939 )

or simply give another file name in SHARED_PREFERENCES_FILE variable.

Also it can be a problem in place where you call loadSharedPreferences(), but I cannot see it in your source.

Update: Notice, that you should not call loadSharedPreferences() in Setup activity, you should call it in your Main activity!

Community
  • 1
  • 1
Aleksandr
  • 1
  • 1