0

I want to design an activity in which I have button with a title "Do not show the screen again in future", on pressing which the splash screen is skipped no matter no how many times user opens the application.
I tried using android shared Preferences (seeing answer for other questions) but I am not getting the desired output. I have given below the code I have used. Please let me know in what way the code must be corrected. If there any other means, I am happy to learn that. Thanks in Advance.

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

        @Override
        public void run()
        {
            try
            {
                sleep(10000);
                if (bRun)
                {
                    startActivity(new Intent(getApplicationContext(), PnbActivity.class));

                }
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
           }
        }
    }
 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) {

            Object valve = null;
            writePreference("disable", valve);
        }

    }


protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);


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

if(!result) {
    // dissable you splash activity here and move to next one
}
thread = new MyThread();
thread.start();}}       
public void skipAct(View v){
Preference preference = new Preference(Note.this);
preference.disableSplash(true);
   Intent i = new Intent(Note.this, PnbActivity.class);
   startActivity(i);

}
Lal
  • 14,726
  • 4
  • 45
  • 70
The AV
  • 129
  • 2
  • 16
  • in your splash screen activity set a `shared preferance` variable as **clicked** and **unclicked** then set the shared preferance value as **clicked** on `button click event`. then every time check the value of shared preferance if it is checked start activity and jump to `main activity` and call finish else keep continueing the the execution of splashcreen – sud Nov 24 '15 at 04:03
  • now check my edited code given – sud Nov 24 '15 at 07:24

2 Answers2

0

try to change you code like below:

...
if(!result) {
 thread = new MyThread();
 thread.start();}}       
 Preference preference = new Preference(Note.this);
 preference.disableSplash(true);
 Intent i = new Intent(Note.this, PnbActivity.class);
 startActivity(i);
}
else
//else if(result)
{

Intent i = new Intent(Note.this, PnbActivity.class);
startActivity(i);
}
...

Also check How do I make a splash screen load once?

NOTE: - Clear preference at time of user session close to get SplshScreen again. hope it works.

Community
  • 1
  • 1
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
0

no need to create a thread, just before starting your splash screen in your splash screen activity check shared pref value as-

public class Splash extends Activity {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 3000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);

Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View view) {

   SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,  MODE_PRIVATE).edit();
   editor.putString("status", "clicked");
   editor.commit();

        }
    });

  SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

String name = prefs.getString("status", "NotClicked");


if(name.equals("clicked"){
            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(Splash.this,pnbActivity.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
            }
    /* New Handler to start the Menu-Activity 
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {

            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(Splash.this,pnbActivity.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();

        }
    }, SPLASH_DISPLAY_LENGTH);
   }
  }  
sud
  • 505
  • 1
  • 4
  • 12
  • The splash screen must never execute if the user clicks the button once no matter how many times the app is started. Will the code what you have written achieve that ? – The AV Nov 24 '15 at 06:23
  • defenitely shared preferance are mean to that. value in shared pref are conserve as long as you dont clear them. no matter how many times you close or opened app – sud Nov 24 '15 at 06:26
  • Sud I tried what you said. But I am not able to resolve the error. Can you please put the entire code? – The AV Nov 24 '15 at 06:51
  • One error is shown now, My MY_PREFS_NAME could not be resolved. How to fix that? – The AV Nov 24 '15 at 12:30
  • `public static final String MY_PREFS_NAME = "MyPrefsFile";` add this – sud Nov 25 '15 at 03:54
  • The solution is working but there is a problem coming. After I install the app and press do not show the screen again, the screen wont displayed when app is opened next time and Activity named PnbActivity is opened. But after 3 or 4 seconds again the same screen PnbActivity gets opened. Why is this happening? – The AV Nov 30 '15 at 07:08
  • It's getting called twice. How to avoid that? – The AV Nov 30 '15 at 11:19