Android - Handler - How To Stop The Intent? - android

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.

Related

After Home Button get pressed i got activities switch ANDROID

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);
}
}

how to check if an application is killed during an animation in android

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

What code should I input if I want to have a timer that will intent to other class?

What code should I put if I want to have a class that has a timer that will intent to other class? but it has a button that will automatically intent to other class if I click the button. its like a game, that if you don't click the button you will be transferred to the other class because it has a time limit
example is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (ImageButton) findViewById(R.id.ib_a);
b = (ImageButton) findViewById(R.id.ib_b);
c = (ImageButton) findViewById(R.id.ib_c);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
Thread timer = new Thread(){
protected void timeout() {
try {
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
Intent intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
}
};start();
}
}//this one
I don't know what code should I input so I put the sleep though I know it is wrong
I have an error on the last bracket I'm very noob in brackets can you help me?
You can use an timeout just like this:
private void timeout() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
}
}, 5000);
I use this method all the time and I never had any problems.
From what I understand you are trying to do is you want to go to the next Activity if the Button is clicked or if the time is up.
that if you dont click the button you will be transfered to the other class because it has a time limit
With your code, the Thread won't run if the Button isn't clicked so you need to move that code outside of the Listener for it to run when the Button isn't clicked, wherever you want it to start.
so i put the sleep though i know it is wrong
sleep() is ok because it is on a background Thread
Use this for reference
private void timeOutCheck() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//call intent here
}
}, 5 * 1000);
}

How can i code a time limit in my android game

i have a quiz game for my android that has a time limit. what i want is there is a choices button that if you click one of the buttons it you will be automatically intent to the class next level but if you didnt answer or click any of the button you will be intent to the other class, thats why the game has a time limit. my problem is i dont know how to put a time limit that will intent or transfer you in another class automatically if you didnt click any of the button choices. i tried sleep but what happen is even i already clicked the correct answer and im on the next level class it will sleep to the class i intented to my sleep. please help me with my problem. i also try handler but didnt work
public class EasyOne extends Activity {
Button a, b, c;
TextView timer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (Button) findViewById(R.id.btn_ea1);
b = (Button) findViewById(R.id.btn_eb1);
c = (Button) findViewById(R.id.btn_ec1);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
private Runnable task = new Runnable() {
public void run() {
Handler handler = new Handler();
handler.postDelayed(task, 5000);
Intent intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
};
You should use a handler but in order to cancel the timeout you must remove the delayed message from the handler in your click listener code.
public class EasyOne extends Activity {
static private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 123) {
((EasyOne) msg.obj).onTimeout();
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (Button) findViewById(R.id.btn_ea1);
b = (Button) findViewById(R.id.btn_eb1);
c = (Button) findViewById(R.id.btn_ec1);
Message msg = mHandler.obtainMessage(123,this);
mHandler.sendMessageDelayed(msg,5000);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"CORRECT!",
Toast.LENGTH_SHORT).show();
mHandler.removeMessages(123,this);
Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
startActivity(intent);
}
});
}
private void onTimeout() {
//your code
}
}

How to redirect from one activity to another after delay

I am developing an android application. In this application, I want to transition from one activity to another activity automatically after 4 seconds. I don't know how to do this without a button.
This is how you can proceed:
int timeout = 4000; // make the activity visible for 4 seconds
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
finish();
Intent homepage = new Intent(Activity1.this, Activity2.class);
startActivity(homepage);
}
}, timeout);
Add the code in your oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(intent);
}
},4000);
}
You can add a Handler in you activity, like:
private Handler handler = new Handler();
Then in your onCreate() method of activity, you can call:
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(yourIntent);
}
}, 4000);

Categories

Resources