Reputation: 9924
I made a splash screen from a tutorial and it works great on Android 2.3+ and even on Android 4.0+ BUT if i ran it on my Galaxy Tab (Android 3.1) it is starting TWICE the application. Really annoying what can i do to solve this on 3.1 ?
Here is the code:
public class SplashScreen extends Activity
{
protected boolean _active = true;
protected int _splashTime = 2000; // time to display the splash screen in ms
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
try{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
overridePendingTransition(0 , 0);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
// finish();
try{
Intent i = new Intent();
i.setClass(SplashScreen.this, MySecondActivity.class);
startActivity(i);
finish();
}
catch(Exception e)
{
ki(e);
}
}
}
};
splashTread.start();
}catch(Exception ex)
{
ki(ex);
}
}
@Override
public void onBackPressed() {
return;
}
//Toaster
public void ki(Exception message)
{
Toast myToast = Toast.makeText(getApplicationContext(), message.toString(), 1);
myToast.show();
}
}
So as you see it is a simple threading activity change, but when it changes, it is starts the MySecondActivity twice.
What the heck?
E D I T:
I just realized, this bug only happens if i start the app on landscape mode while it is on splashing/loading screen. Maybe it is a valuable info to solve this..
Upvotes: 0
Views: 366
Reputation: 1786
I don't think you should use sleep. Maybe you could try this instead.
private void popSplash() {
Message msg = splashHandler.obtainMessage();
splashHandler.sendMessageDelayed(msg, 2000);
}
private Handler splashHandler = new Handler() {
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
startActivity(new Intent(SplashScreen.this,MySecondActivity.class);;
finish();
}
};
just call the popSplash in onCreate.
This worked for me -
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Display d = getWindowManager().getDefaultDisplay();
// For tablets, to avoid dublicate onCreates.
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
Upvotes: 2
Reputation: 15402
public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
Intent i = new Intent(SplashScreen.this, MyApp.class);
startActivity(i);
}
}
};
splashTread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
Upvotes: 0