0

I have designed a splash screen. The Java code is as below. In that screen, I have a button named "Do not show this screen again future". On pressing this button, the splash screen must never been shown in future, no matter how many times the app is started. How can I achieve this? Thanks in advance.

public class Qz1 extends Activity {
MyThread thread;
private class MyThread extends Thread
{
    public boolean bRun = true;

    @Override
    public void run()
    {
        try
        {
            sleep(3200);
            if (bRun)
            {
                startActivity(new Intent(getApplicationContext(), Qone.class));
                Qz1.this.overridePendingTransition(R.anim.newright,
                        R.anim.newleft);
            }
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}


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

    thread = new MyThread();
    thread.start();
}


public void round1(View v){

       Intent i = new Intent(Qz1.this, Qone.class);
       startActivity(i);
        this.overridePendingTransition(R.anim.newright,
                R.anim.newleft);
    }
}
4ae1e1
  • 7,228
  • 8
  • 44
  • 77

3 Answers3

0

Use SharedPreferences for this.

You can save a persisted boolean value by calling

getPreferences(MODE_PRIVATE).edit().putBoolean("no_splash", true).commit();

Then you can check that value by calling

boolean noSplash = getPreferences(MODE_PRIVATE).getBoolean("no_splash", false);

If noSplash is true then launch your main Activity immediately rather than starting the Thread.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26
  • There are few errors popping up. Can you please add the piece of code which you have mentioned in the comment to the code in the question. This can be useful to solve the errors. – Suresh Anand Nov 19 '15 at 05:43
0

User shared Preferences to achieve this, create a class

public class Preference {

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public Preference(Context context) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void writePreference(String key, Object value) {
        if(value instanceof Boolean) {
            editor = sharedPreferences.edit();
            editor.putBoolean(key, (Boolean) value);
            editor.commit();

        }
    }

    public Object readPreference(String key , Object defValue) {

        if(defValue instanceof Boolean)
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        else
            return null;
    }

    public Boolean getDisableSplash() {
        return (Boolean) readPreference("disable", false);
    }

    public void disableSplash(Boolean value)) {
        writePreference("disable", valve);
    }

}

and in your main create an object of Preference to read and write preference

Preference preference = new Preference(YourActivity.this);
Boolean result = preference.getDisableSplash();

if(!result) {
    // dissable you splash activity here and move to next one
}

and when you want to disable it simply

Preference preference = new Preference(YourActivity.this);
preference.disableSplash(true);
Zubair Akber
  • 2,760
  • 13
  • 30
0

You can solve this by creating delegate activity, make an empty activity an set it as launcher activity,

On the delegate activity oncreate check your preference if you should show the splash finish the delegate activity and show it else show your home screen.

Omar Hassan
  • 727
  • 1
  • 11
  • 24