Question

I have app starting with Splash screen then go to MainActivity depend on user preference which determined by chose any option in listpreference :

1- start app without splash and music .

2- start app with splash only .

3- start app with splash and music .

i implement the listpreference but i cant add sharedPreferences to each values so after check any of them it stored in sharedPreferences and start app depend on user preference,

as this is first time to use listpreference and im new to android i don't know how to do it,

any help please will be appreciated .

Splash.java

 public class Splash extends Activity{  
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);     
    ourSong.start();

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(2000); 
                }
              catch (InterruptedException e){
                e.printStackTrace(); 
                }
              finally{
                  Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                  startActivity(intent); 
                  }
            }                                   
        };
         timer.start();   
         }  
@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

Prefs.java

public class Prefs extends PreferenceActivity{

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

    addPreferencesFromResource(R.xml.prefs); 
    }
}

prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">

     <ListPreference
       android:title="Application Start Preference"
       android:summary="This preference allows to select how the app will start"
       android:key="listPref"
       android:entries="@array/splash"
       android:entryValues="@array/splash_values" />          
</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>   
  <string-array name="splash">
      <item>start app without splash screen </item>
      <item>start app with splash scrren only</item>
      <item>start app with splash screen and music</item>
  </string-array>

  <string-array name="splash_values">
      <item>1</item>
      <item>2</item>
      <item>3</item>

  </string-array>    
</resources>

UPDATE: i tried the below code but whats happen is app run normally start with splash then go to mainactivity then i press preference in option menu its OK it open preference page then press on listpreference it rise the dialog of 3 radiobutton and if i press any one of radiobuttons its ok , then i back and back till exit app , then open it again it open directly to show the mainactivity without splash then crashes immediately.

also line 23 in logcat is:

 int splashType = getPrefs.getInt("listPref", 0);

Splash.java

public class Splash extends Activity{  
 MediaPlayer ourSong;
 @Override
  protected void onCreate(Bundle savedInstanceState) {
   this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
   // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);  

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences
             (getBaseContext());

 int splashType = getPrefs.getInt("listPref", 0);
  switch (splashType) {
  case 2:
Intent intent = new Intent(Splash.this, MainActivity.class);                                     
       startActivity(intent);
  break;

  case 1:
   setContentView(R.layout.splash);  
    Thread timer = new Thread()
   {
    public void run()
    {
        try
        {
            sleep(2000); 
        }
        catch (InterruptedException e)
        {
            e.printStackTrace(); 
        }
        finally
        {
            Intent intent = new Intent(Splash.this, MainActivity.class);                                     
            startActivity(intent);  
         }
      }                          
    };
   timer.start();       
break;

 case 0:
   ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
   ourSong.start();

Thread timer1 = new Thread(){
    public void run(){
        try{
            sleep(2000); }
          catch (InterruptedException e){
            e.printStackTrace(); }
          finally{
          Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent); 
                    }
                   }                                    
                 };
           timer1.start();   
  break;
   }
  }
@Override
protected void onPause() {
     // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
      } 
   }

LOGCAT:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.Splash}: java.lang.ClassCastException: java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: java.lang.String
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2968)
at com.tsn.dr.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
Was it helpful?

Solution

You can try something like that to get the stored user preference and then take relevant action:

SharedPreferences sharedPrefs;
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String userSplashValue = sharedPrefs.getString("listPref", "1");

if (userSplashValue.equals ("1")) {
 // choice 1
}
else if (userSplashValue.equals("2")) {
 // choice 2    
}
else if (userSplashValue.equals ("3")){
 // choice 3    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top