I created two Activities "SplashActivity and AuthenticationActivity" When I start the app it loads the splash screen for 5 seconds and then it moves on to AuthenticationActivity. My problem is that when I run the app, it loads the splash screen, but within 5 seconds when I click the back button, SplashActivity exits, and immediately AuthenticationActivity appears in the foreground.
Does anyone know how to make it so when I click the back button, my app exits?
Here's my code so far:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTimer = new Thread(){
public void run(){
try{
sleep(5000);
startActivity(new Intent(SplashActivity.this,AuthenticationActivity.class));
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
splashTimer.start();
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}}
Timer timer;
TimerTask timerTask;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
timer = new Timer();
timerTask = new TimerTask()
{
#Override
public void run()
{
//Start your activity here
}
};
timer.schedule(timerTask,2500);
}
#Override
public void onBackPressed() {
super.onBackPressed();
timer.cancel();
}
or you can use Handler also
Handler handler;
Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initialize();
}
private void initialize()
{
handler = new Handler();
runnable = new Runnable()
{
#Override
public void run()
{
//start your activity here
}
};
handler.postDelayed(runnable, 5000);
}
#Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(runnable);
}
Related
I have my first activity that switch to the second one after 3 seconds and this works fine. The problem is that if i press the Home Button during this 3 seconds, the app reopen in the second activity. Is there a simple way to fix this?
Thanks in advance.
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
}
EDIT:
Maybe I wasn't clear, I do not want the app to reopen once I press the home button. How can I do this?
This is because you don't clear your delayed callback. You can fix it in this way:
private Handler handler = new Handler();
public void switchActivities() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
public void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
You just have to cancel your Handler when you leave your first activity before the second one opens.
public class StartActivity extends AppCompatActivity {
Runnable nextActivityRunnable;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
switchActivities();
}
#Override
public void onBackPressed(){
}
public void switchActivities() {
nextActivityRunnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(nextActivityRunnable, 3000);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(nextActivityRunnable);
}
}
I have two activities(MainActivity,Sample) which extend to a custom activity(BaseActivity) i have created. I have created a handler which runs a runnable every 5 seconds in BaseActivity. In BaseActivity i have overridden onpause and onresume methods to start and stop handler whenever needed. When i remove the callbacks of handler in onPause of BaseActivity, the handler stops but won't stop in onpause of MainActivity. Below is the code.
public class BaseActivity extends Activity{
public int mInterval = 5000;
public Handler mHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Oncreate baseactivity");
mHandler = new Handler();
}
Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
Log.d("MyApp", "every 5 secs");
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
#Override
protected void onPause() {
super.onPause();
System.out.println("OnPause baseactivity");
//stopRepeatingTask();
}
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
#Override
protected void onResume() {
super.onResume();
System.out.println("Onresume baseactivity");
startRepeatingTask();
}
}
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newpage(View v){
startActivity(new Intent(this,Sample.class));
}
#Override
protected void onPause() {
super.onPause();
System.out.println("onpause mainactivity");
stopRepeatingTask();
}
}
public class Sample extends BaseActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.txt);
tv.setText("sample activity");
}
}
The Onpause of both BaseActivity and mainActivity are called but the handler still runs if the app is closed. I hope am clear.
I think to start thread we should use start() method,
and write onPause method of MainActivity in BaseActivity Itself and remove in MainActivity(i mean stopRepeating Task).
I hope it works.
In my app i set the splash screen timer to 5 sec and later on think that 5 sec is too long so i change it back to 1 sec and my splash screen doesn't seen on the screen and keep me waiting for more than 5 sec i couldn't find what is wrong so here is my Splashscreen code
public class Splash extends Activity
{
private Timer_Countdown timer_Countdown = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer_Countdown = new Timer_Countdown(5000, 1000);
timer_Countdown.start();
}
class Timer_Countdown extends CountDownTimer
{
public Timer_Countdown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timer_Countdown.cancel();
Intent startIntent;
startIntent = new Intent("android.intent.action.MAINMENU");
startActivity(startIntent);
}
#Override
public void onTick(long millisUntilFinished) {
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
And one last thing if I change it back to 5 sec it shows up on the screen again.
Why you are using this much of code just to use splash screen. Make it simple, you can use below code.
public class Splash extends Activity {
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
timer.schedule(new TimerTask() {
public void run() {
Intent intent = new Intent(Splash.this, NewActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
You can use Handler also
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplashActivity.this, YourNewActivity.class));
finish();
}
}, 3000);
or Using Timer with Timer Schedule
public class Splash extends Activity {
Timer t= new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
t.schedule(new TimerTask() {
public void run() {
Intent n= new Intent(Splash.this, YourNewActivity.class);
startActivity(n);
}
}, 3000);
}
}
Use this instead of Timer
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//code for starting new activity
}
}, 5000);
I want to reload my activity after every 1 minute. I tried using handler for this... but problem is that when i press back key of device, it doesn't stop and goes into an infinite loop..
here is my code what i have done--
public class Chat extends Activity {
Handler handler;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.chat);
handler = new Handler();
Toast.makeText(getApplicationContext(), "again", 400).show();
doTheAutoRefresh();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = getIntent();
startActivity(intent);
finish();
//doTheAutoRefresh();
}
}, 10000);
}
public void onPause() {
super.onPause();
}
}
You should do this to remove all handler's messages and callbacks:
public void onPause() {
super.onPause();
handler.removeCallbacksAndMessages(null);
}
I made a splash image to show at the start of my activity..
The image show perfectly.But the problem is when i call this
public class SplashImageActivity extends Activity {
protected boolean active = true;
protected int splashTime = 5000; // time to display the splash screen in ms
/** 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 {
startActivity(new Intent(SplashImageActivity.this,Myapps.class));
finish();
//startActivity(new Intent("com.splash.com.MyApps"));
//startActivity( new Intent(getApplicationContext(), Myapps.class));
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
active = false;
}
return true;
}
}
go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this
what's the problem?
No need to call stop() and call finish() after starting activity
finally
{
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
I use thread to show the Splash screen, and it works for me:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
wait(4000);
}
}catch(InterruptedException ex){
}
finish();
Intent i=new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
interrupt();
}
};
mSplashThread.start();
}
Please try below code..
public class Splashscreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread t2 = new Thread() {
public void run() {
try {
sleep(2000);
startActivity( new Intent(getApplicationContext(), Exercise.class));
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t2.start();
}
}
No need to call stop() just call finish() after starting activity
finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
You can also use handler an postdelayed() to make a splash screen like below
public class SplashScreenActivity extends Activity{
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
final Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
}
}
You will show your splash screen for 5 seconds and then move to next Activity
first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........
as looks you try some thing like this link