0

That's my first question and I'm an Android noob yet (hopefully), so please try to forgive me if I'm asking stupid questions.

I'm working on an Android application and I have to make a splash screen. I made one using this answer: How do I make a splash screen? . It is working very fine, but... this solution makes another big thread for the rest of application and I am trying to avoid that - I think it slows whole application (another app thread). Am I right?

I tried to invert whole process - I'm invoking MainMenu activity, then making another thread just for splash:

public class MainMenu extends Activity implements OnItemClickListener { 

private GridView gridView;
private AlertDialog.Builder dialog;
private Intent intent;
private ApplicationPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    prefs = new ApplicationPreferences(this);
    setTheme(prefs.GetApplitacionTheme());
    SQLDatabase.onCreate();

    if (prefs.SplashScreenEnabled()) {
        new Runnable() {

            @Override
            public void run() {
                startActivity(new Intent(MainMenu.this, SplashScreen.class));                   
            }
        }.run();
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    gridView = (GridView)findViewById(R.id.gridView);
    gridView.setAdapter(new AdapterMainMenu(this));
    gridView.setOnItemClickListener(this);
}

Then in my SplashScreen activity:

public class SplashScreen extends Activity {

private Locale locale;
private Configuration config;
private ApplicationPreferences prefs;

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

@Override
protected void onResume() {
    try {
        Thread.sleep(2500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finish();               
}

After onCreate() Android calls onResume() so I decided to pause thread here and after that finish activity.

When application comes back to the main thread it crashes and I don't know why. What am I doing wrong?

Thanks in advance! Unguis

Community
  • 1
  • 1
  • Post your LogCat errors. Why is `startActivity()` in a Runnable? You should use a Runnable to close the SplashScreen after 2.5 seconds instead of using `sleep()`. – Sam Apr 04 '13 at 18:06
  • Your classes naming convention (MainMenu is activity not menu as name indicates) will give you troubles in bigger project... – Marcin Orlowski Apr 04 '13 at 18:07
  • Thanks! I will change it to avoid problems. – Unguis Prime Apr 04 '13 at 18:15

2 Answers2

0

You don't want to call Thread.sleep() in onResume() because that blocks the UI thread. The solution that you tried first with Handler.postDelayed() is definitely the preferred way. Why do you think it creates a "big" thread? Runnable.run() is actually called in the main UI thread with that solution.

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Whole app is working in another thread made with Handler.postDelayed() as I think till moment when I end MainMenu activity (app comes back to the SplashScreen where MainMenu was invoked) and then it ends with finish(). I thought it slows whole app. Isn't it? – Unguis Prime Apr 04 '13 at 18:12
  • if you use the Handler like that, it will always execute the Runnable in the UI Thread, so no other thread is created. Btw: in Android, all the Activity live-cycle methods, like onCreate(), onResume() etc., are always executed in the UI Thread as well. – SimonSays Apr 04 '13 at 20:28
0

You can use a timer and a handler as below

public class SplashScreen extends Activity{

Timer splashTimer;
SplashTimerHandler splashTimerHandler;

private boolean applicationPaused=false;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    this.setSplash();


}

private void setSplash() 
{
    this.splashTimerHandler=new SplashTimerHandler();
    this.splashTimer=new Timer();

    this.splashTimer.schedule(this.splashTimerHandler, 0, 1000);

}

@Override
public void onPause()
{
    super.onPause();
    this.applicationPaused=true;
    this.closeSplashTimer();
}

@Override
public void onResume()
{
    super.onResume();

    if(this.applicationPaused)
    {
        this.applicationPaused=false;
        this.closeSplashTimer();
        this.setSplash();
    }
}

public class SplashTimerHandler extends TimerTask{

    int splashTimerCounter=0;
    @Override
    public void run()
    {
        splashTimerCounter++;
        if(splashTimerCounter>2)
        {
            runOnUiThread(splashTimeOver);
        }       
    }

    private Runnable splashTimeOver=new Runnable() {

        @Override
        public void run()
        {
            closeSplashTimer();
            startHomeScreen();
        }
    };      
}

protected void closeSplashTimer() 
{
    if(this.splashTimer!=null)
    {
        this.splashTimer.cancel();
        this.splashTimer=null;
    }

}

private void startHomeScreen() 
{
    this.closeSplashScreen();
    startActivity(new Intent("com.example.MainActivity"));

}

private void closeSplashScreen()
{
    this.closeSplashTimer();
    this.finish();

}

@Override
public boolean onKeyDown(int keycode, KeyEvent event)
{
    if(keycode==KeyEvent.KEYCODE_BACK)
    {
        this.closeSplashScreen();
    }
    return true;
}

}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256