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);
}
}
Related
I have a long running operation. Inside a thread I start a new activity like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
}
}).start();
}
The problem is when I run another app and the thread finishes, that activity appears on the top of the screen. How to force that activity not to apear on the top?
you can use like below..
Handler splashHandler;
splashHandler = new Handler();
splashHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent loginIntent = new Intent(Splash.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
}, 2000);
#Override
public void onBackPressed() {
super.onBackPressed();
if (splashHandler != null)
splashHandler.removeCallbacksAndMessages(null);
}
#Override
protected void onResume() {
super.onResume();
if (splashHandler != null)
splashHandler.removeCallbacksAndMessages(null);
}
This happens because you're getting the Context out of the "other app". You should pass in the application context rather than a context from the local activity. Use context.getApplicationContext() and save that in a local variable, then use this context to start the activity
The code should be something like this:
#Override
protected void onPostExecute(List<VideoDataDescription> result) {
super.onPostExecute(result);
MainActivity.progressDialog.dismiss();
context.startActivity(new Intent(context, MainActivity.class));
}
}
you'd call it like this:
new MyTask(context).execute();
I want to stop the delay/intent from processing when the back button is pressed. I've searched some threads but I don't have the same Handler logic, I think. I apologize if it is.
Here is the code I'm working with:
else {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, PollWebView_Gilmore.class);
startActivity(intent);
finish();
}
}, 10000);
You have to save a reference to both the Handler and your Runnable somewhere, and then you can use the removeCallbacks(Runnable) method on the Handler to cancel the pending request.
Example code:
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.postDelayed(runnable, 5000);
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(runnable);
}
}
If you Don't need a delay why do you use a Handler? Just remove it!
Call this directly
Intent intent = new Intent(MainActivity.this, PollWebView_Gilmore.class);
startActivity(intent);
finish();
When your back button is pressed!
You can also get an idea by reading below posts.
Android: Proper Way to use onBackPressed() with Toast
How to run a Runnable thread in Android?
If you want to do something with the intent on back pressed.You can override onBackPressed() method.
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);
}
i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer
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);
}