android time delay between views - android

I'm not sure, what is stopping this from working. I have the code setup to cause a 3 second time delay but the View isn't working, it stays black and then after 3 seconds switches to the next screens. I think, I am doing the time delay and something hasn't been called within Android to display the layout...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
start = System.currentTimeMillis();
setContentView(R.layout.team);
}
protected void onStart()
{
super.onStart();
while(game)
{
now = System.currentTimeMillis();
if (now - start >= 5000)
{
game = false;
Intent about = new Intent(this, SplashScreen.class);
startActivity(about);
}
}
}

I believe you want to implement a Screen with few seconds delay and then start your main Application. Just like a Splash Screen Before the Main Application Starts Right?
Then this will help you!
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** set time to splash out */
final int welcomeScreenDisplay = 4000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
startActivity(new Intent(CurrentActivity.this, NextActivity.class));
finish();
}
}
};
welcomeThread.start();
}
This is a Screen for 4 seconds of Delay.

You should be using the Timer class to launch the new activity.

Related

Kill intent on splash screen

this is my Splash Screen, If I press home or multitasking/appswitch button when Intent is started app crash, in logcat is FATAL EXEPTION: Thread-1277. Can I kill/delete this Intent when player press home button?
public class SplashScreen extends Activity {
private static int loadingTime = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, loadingTime);
}
}
The following code tracks whether the SplashActivity is at least partially showing. If yes, it will continue to MainActivity. If not (activity is finished by pressing Back button, activity is stopped by pressing Home button) nothing happens.
This solution uses Fragments so the timing is preserved across e.g. screen orientation changes (it will always take specified time no matter how many times you rotate your device - the timer will not reset).
public class SplashActivity extends Activity {
// tracks when the activity is at least partially visible (e.g. under a dialog)
private boolean mStarted = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your current code
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_startup);
if (savedInstanceState == null) {
// first time onCreate, create fragment which starts countdown
getFragmentManager()
.beginTransaction()
.add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG)
.commit();
} else {
// fragment already set up from first onCreate after screen rotation
}
}
#Override
protected void onStart() {
// the activity becomes at least partially visible
mStarted = true;
super.onStart();
}
#Override
protected void onStop() {
// the activity is no longer visible
mStarted = false;
super.onStop();
}
public boolean isStarted2() {
// there is already hidden method isStarted() in the framework
// you can't use it and are not allowed to override it
return mStarted;
}
public static class SplashFinishFragment extends Fragment {
private static final String TAG = SplashFinishFragment.class.getSimpleName();
private static final int DELAY = 1000; // one second delay
private static final Handler mHandler = new Handler(); // one main thread anyway
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
if (getActivity() == null) {
// this should never happen, there is no activity, so no fragment
Log.e(TAG, "No activity!");
return;
}
SplashActivity a = (SplashActivity) getActivity();
if (a.isStarted2() || a.isChangingConfigurations()) {
// if activity is even partially visible or is rotating screen right now, continue
Intent i = new Intent(a, SettingsActivity.class);
a.startActivity(i);
}
// in any case close splash
a.finish();
}
};
public static SplashFinishFragment newInstance() {
return new SplashFinishFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the countdown will continue (not reset) across screen rotations
setRetainInstance(true);
// try running the main activity after specified time
mHandler.postDelayed(mRunnable, DELAY);
}
#Override
public void onDestroy() {
// if the fragment gets destroyed (e.g. activity closes) do not launch main activity
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
}
This was tested on a virtual Galaxy S2. It works when Home or Back button is pressed. It doesn't work when Recent Apps button is pressed. I don't know your use case but personally I would expect the app to continue launching while I browse recent apps.

MainActivity starts before SplashScreen ends

I have a SplashScreen activity at my android app that lasts for 3 seconds, and then it switches to MainActivity.
MainActivity plays a sound, and it has a button. My problem is that the sound plays when the SplashScreen activity is visible.
onResume at MainActivity calls for run(), run() sleeps for 2 seconds and plays the sound.
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
m.start();
try {
Thread.sleep((int) randomValue * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.seekTo(0);
m.start();
cpu.setText(EranThatWillShortYourDouble(randomValue));
btn.setClickable(true);
}
#Override
protected void onResume() {
super.onResume();
run();
}
Instead of SplashScreen displaying for 3 seconds and switches to MainAcivity, It lasts 5 seconds and plays the sound when SplashScreen is visible.
What do I do?
EDIT: This is my SplashScreen activity:
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Please take a look at AsyncTasks, Android Runnables and the ability to use a Handler to postDelayed.
Don't do thread.sleep in the main thread, it's really bad. :)
Do not use splash screens in Android. They are against the design guidelines and provide a poor UX.
http://developer.android.com/design/patterns/help.html#your-app

Splash screen for Android tab bar application

I have created the android application with tabhost. In this application, I am having the 4 tabs and each one contains their separate webview. For this application, I want to add SplashScreen for the application before the tab bar's webview is loaded. How can I achieve this?
Create a different activity to show the splash which will be your launcher activity. After launching you can start the tabhost from this activity
Try this
public class SplashScreen extends Activity {
// time for splashscreen
protected int _splashTime = 5000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
// Go to Main activity
Intent i = new Intent();
i.setClass(sPlashScreen, MainActivity.class);
startActivity(i);
finish();
}
}
};
splashTread.start();
}
}
Hope it helps.

Activity gets killed when orientation is changed

I have two activities in my project- Splash and AActivity. Splash is the main activity and is working fine. But if i change the orientation while Splash activity is running, the UI of splash activity goes off but it opens the AActivity after 10 sec.
code for splash activity is -
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run() {
try{
sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent AActivityIntent = new Intent("com.example.ex.AACTIVITY");
startActivity(AActivityIntent);
}
}};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
I want to retain the UI of splash activity for 10 seconds even if orientation is changed. After 10 sec splash activity should be finished. How to do it ???
I suggest you doing the following:
public class Splash extends Activity {
private Thread timer;
private volatile long timeLeft;
private long timeStarted;
private long timeStopped;
private static final long TIME_TO_SHOW = 100000
private static final String KEY_TIME_LEFT = "timeLeftToRun";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
if (savedInstanceState != null) {
timeLeft = savedInstanceState.getLong(KEY_TIME_LEFT, 0);
} else {
timeleft = TIME_TO_SHOW;
}
timer = new Thread() {
public void run() {
try {
sleep(timeLeft);
Intent AActivityIntent = new Intent("com.example.ex.LISTSCREEN");
startActivity(AActivityIntent);
} catch(InterruptedException e) {}
}
}};
timeStarted = SystemClock.elapsedRealtime();
timer.start();
}
#Override
protected void onPause() {
super.onPause();
timer.interrupt();
timeStopped = SystemClock.elapsedRealtime();
finish();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
timeLeft -= timeStopped - timeStarted;
if (timeLeft > 0) outState.putLong(timeLeft);
}
}
The main idea here is that you kill the thread if the activity is killed, but you take a note for how long it has run and how much time it has left. When the activity is restored, you do the same actions, except you have to wait for a smaller amount of time.
The code above is, of course, untested, but it should illustrate the idea.
Insert into your manifest these below block.
It means orientation change situation controlled by your "activity".
http://developer.android.com/guide/topics/manifest/activity-element.html#config
android:configChanges="orientation"
And more.
Override "onConfigurationChanged" method.
Try this. You can do everything you want.
Instead of using a Thread for delayed launch of activity use Alarmmanager, even if you are quitting the app you could always cancel the pending Alarm

Android transistions not working

I have a start screen with a short delay, after the delay it moves onto the splashscreen however I cant seem to override the transition and I cant see why...Although the transition does work in debug mode which is very strange
package com.example.android.bubblestrouble;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Team extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final int delay = 3000;
setContentView(R.layout.team);
Thread welcomeThread = new Thread()
{
int wait = 0;
#Override
public void run()
{
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < delay)
{
sleep(100);
wait += 100;
}
}
catch (Exception e)
{
System.out.println("EXc=" + e);
}
finally
{
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
startActivity(new Intent(Team.this, SplashScreen.class));
Team.this.finish();
Team.this.overridePendingTransition(R.anim.fade, R.anim.hold);
}
}
};
welcomeThread.start();
}
}
Straight solution. Show your splash image immediately on activity start and replace in with the needed layout by timer. Something like this(onCreate event of Team activity):
ImageView splashImage = new ImageView(this);
splashImage.setImageBitmap(splashBitmap); // or drawable/resource - as you like
setContentView(splashImage);
Thread timerThread = new Thread() {
#Override
public void run() {
sleep(3000);
Team.runOnUiThread(new Runnable() {
#Override
public void run() {
setContentView(R.layout.team);
}
});
}
}
timerThread.start();

Categories

Resources