How to display an activity automatically after 5 seconds? - android

In my application I have created a splash screen type of thing in Android. It should remain for 5 seconds.
My problem is how do I display another activity automatically after 5 secs?
The splash screen doesn't have a button, rather it should display another activity automatically after 5 seconds without the click of a button.

new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent mainIntent = new Intent(LaunchActivity.this, HomeActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}, 5000);

TimerTask task = new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainMenu.class);
startActivity(intent);
finishscreen();
}
};
Timer t = new Timer();
t.schedule(task, 5000);
and
private void finishscreen() {
this.finish();
}

You can use thread here
For example
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(500);
if(_active) {
waited += 500;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
// start your activity here using startActivity
stop();
}
}
};
splashTread.start();

This can also be done using android CountDownTimer class.
See this example for 5seconds delay.
new CountDownTimer(5000, 1000) {
public void onFinish() {
Intent startActivity = new Intent(ThisActivity.this,ActivityToStart.class);
startActivity(startActivity);
finish();
}
public void onTick(long millisUntilFinished) {
}
}.start();
You may also need to define your parent activity in AndroidManifest.xml file,
<activity
android:name=".ActivityToStart"
android:label="Back"
android:parentActivityName=".MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>

Related

how to stop open activity while splash screen killed

I have splash screen .
once i open my application the splash screen will appears after completion of splash screen passed intent to HomeActivity.
but when i kill this app while splash screen running after some time HomeScreen will automatically open , but i want to kill the app.
but the HomeScreen should not show when i killed the app .
public class SplashAnimation extends Activity {
ImageView imageViewSplash;
TextView txtAppName;
RelativeLayout relativeLayout;
Thread SplashThread;
MediaPlayer mySong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_view);
mySong=MediaPlayer.create(SplashAnimation.this,R.raw.monn);
mySong.start();
imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash);
txtAppName = (TextView) findViewById(R.id.txtAppName);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
startAnimations();
}
private void startAnimations() {
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.translate);
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
rotate.reset();
translate.reset();
relativeLayout.clearAnimation();
imageViewSplash.startAnimation(rotate);
txtAppName.startAnimation(translate);
SplashThread = new Thread() {
#Override
public void run() {
super.run();
int waited = 0;
while (waited < 3500) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited += 100;
}
SplashAnimation.this.finish();
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
mySong.stop();
}
};
SplashThread.start();
}
#Override
protected void onStop() {
SplashAnimation.this.finish();
finish();
mySong.stop();
super.onStop();
}
#Override
protected void onDestroy() {
finish();
mySong.stop();
super.onDestroy();
}
}
Once you have called SplashThread.start() it will do its job as long as it can do. I would recommend to use a Handler instead, tho you can remotely cancel the task, the Handler runs:
//init and declare the handler instance
private Handler delayHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (delayHandler == null) {
delayHandler = new Handler();
}
//your code
}
//define the task the handler should do
private void startAnimations() {
//replace the code beginning at 'Thread SplashThread = new Thread()' with the following
delayhandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
//these flags will prevent to 'redo' the transition by hitting the back button, that also makes calling 'finish()' obsolete
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
//instead of the while loop just execute the runnable after below given amount of milliseconds
}, 3500)
//to remotely cancel the runnable, if the app, respectively the Activity gets killed override 'onDestroy()'
#Override
public void onDestroy() {
super.onDestroy();
mySong.stop();
//calling 'finish()' is obsolete, tho 'finish()' calls 'onDestroy()' itself
//tell the handler to quit its job
delayHandler.removeCallbacksAndMessages(null);
}
Call in onStop() method
SplashThread.interrupt()
You can use Timer instead of instantiating the Thread class.
Refer the code below to start the Activity after 4 seconds. Use this in onCreate() of SplashActivity.
timer = new Timer().schedule(new TimerTask() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}, 4000);
In your onPause() method use:
timer.cancel()
This will terminate the timer and disregards any currently scheduled tasks.

How to start activity after some time in Android?

I am developing and android application in that I want to start another application(my second application). I am doing the following code
Intent i= new Intent();
i.setComponent(new ComponentName("my second app package","my class name"));
startActivity(i);
It is working fine.
I want to hide the second application after 3 or 5 seconds for that I am following the below code
Timer t=new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent("first app package","first app class name" );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 5000);
But this code is not working as I expected only for my second application. But other applications are working fine. Instead thread I also tried Handler, alaram manager also no sucess. Please any one help me in this.
Is I want to do any code change in my second application or what is the problem with my code?
Thanks
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// your code to start second activity. Will wait for 3 seconds before calling this method
startActivity(new Intent(FirstActivityClass.this,SecondActivityClass.class));
}
}, 3000);
Use above code after onCreate of first activity
Try postDelayed or Timer method
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
//Intent i = new Intent("first app package","first app class name" );
Intent i = new Intent (this , SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 100);
// Time method
import java.util.Timer;
...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Your database code here
}
}, 2*60*1000);
you can use thread but main thing is you have to call finish() method to finish current activity.
Thread thread = new Thread( new Runnable() {
#Override
public void run() {
try
{
Thread.sleep(3000);
}
Intent intent = new Intent(1stActivity.this, SecondActivity.class);
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
finish();
}
}
});
thread.start();
in the try block i put Thread.sleep(3000) you can change it do your work in try block
Try this one:
try{
Thread.sleep(3000);
startActivity(secondActivityIntent);
}catch(Exception e){
//print error here
}
You want the second application to hide after few seconds, Please put this code inside onCreate() of your second application's main Activity:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
YourActivity.this.finish();
}
}, 3000);
handler.post(new Runnable() {
#Override
public void run() {
//Your Code here
}
});
Put this code inside run method of your timer task.And it will work.Hope it will help.Thanks.

Switch from One Activity to Another Activity After a Time Interval

I am creating a New Android application
I d like to switch from one activity to another activity after a time interval, How can i do this?
Kindly guide me
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app Next activity
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(i);
// close this activity
finish();
}
}, TIME_OUT);
There are numerous ways to do this.
You could use postDelayed(), however that is not advised since you cannot STOP it, or control it reliably, between various phases of activity lifecycle, to prevent for example wierd behaviour when the user exits the activity, before the delay has passed.
You would need some locks, or other mechanism.
Most proper approach would be to simply start a timer on the 1st activity onPostResume() which will start another activity after some delay.
TimerTask mStartActivityTask;
final Handler mHandler = new Handler();
Timer mTimer = new Timer();
#Override
private protected onPostResume() { // You can also use onResume() if you like
mStartActivityTask = new TimerTask() {
public void run() {
mHandler.post(new Runnable() {
public void run() {
startNewActivity(new Intent(MyClass.class));
}
});
}};
// This will start the task with 10 seconds delay with no intervals.
mTimer.schedule(mStartActivityTask, 100000, 0);
}
private void startNewActivity(Intent i) {
mTimer.cancel(); // To prevent multiple invocations
startActivity(i); // Start new activity
// finish(); // Optional, depending if you want to return here.
}
Try this code
private Thread thread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
thread = new Thread(this);
thread.start();
}
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent userName = new Intent(this, UserNameActivity.class);
startActivity(userName);
}

Android: How to pause the launch of next activity if user navigates away from current activity?

I have created a splash screen for my application. After 5 seconds it starts the next activity using the below code. Now my problem is, if user navigates away from current activity before 5 seconds are over, then as soon as 5 seconds are over the next activity (in my case InfoActivity) comes in front even if I am in another application or anywhere else.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch);
new Handler().postDelayed(new Runnable() {
public void run() {
final Intent mainIntent = new Intent(LaunchActivity.this, InfoActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}, 5000);
}
you could use a variable
shouldNavigate=true;
that you unset in the onDestroy() method of your original activity.
onDestroy() {
shouldNavigate=false;
[...]
}
In your postDelayed-run()-method you then check
if(shouldNavigate) {...}
This procedure worked for me.
flag = false;
runnable = new Runnable() {
public void run() {
if(!flag) {
final Intent mainIntent = new Intent(LaunchActivity.this, InfoActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
onPause() {
super.onPause();
flag = true;
handler.removeCallbacks(runnable);
}
onRestart() {
super.onRestart();
flag = false;
handler.postDelayed(runnable, timeOfPause-timeOfCreate);
}

Splash screen issue

I am creating a splash screen using the following code ,when i press back key the application moves to the home screen and within a few seconds shows my next mainmenu screen.I am calling finish() in onBackPressed(),I want to close the app on pressing back key in the splash screen.can any one help me on this??
Thanks!!
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < 2000)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("next activity"));
stop();
}
}
};
splashThread.start();
It's because you call finish(); before the startActivity(new Intent("next activity"));
Swap finish(); with startActivity(new Intent("next activity"));
It is working in my application
public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
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 {
if(!stop){
startActivity(new Intent(Splash.this,Home.class));
finish();
}
else
finish();
}
}
};
splashTread.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(splashTread.isAlive())
this.stop = true;
}
return true;
}
}
this solution only solves the problem for the back-button. If users press the home-button the unwanted behavior will still occur. Wouldn't it be easier to overwrite the onStop method and do your thing in there?
#Override
public void onStop(){
super.onStop();
if(splashTread.isAlive())
this.stop = true;
}
Try using this:
SplashScreen.this.finish();
where SplashScreen is the name of the Activity.
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
handler=new Handler();
handler.postDelayed(() -> {
Intent intent=new Intent(SplashActivity.this, Home.class);
startActivity(intent);
finish();
},3000);
}
}
AndriodManifest.xml
<activity
android:name=".SplashActivity"
android:theme="#style/AppTheme"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories

Resources