I want to change from my current activity to another while I am inside a Handler. The idea is other code inside the handler will run until a certain condition doesnt match (with increment of the count value every time). When the count value matches the condition I close the activity and move to another.
my code is:
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
if(count<CONDITION_VALUE)
{
//do other stuff...
count++;
}else
{
//change activity...
finish();
}
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (1000));
The code is running without any error but the old activity is not being destroyed (i guess) and the new activity is reloaded after every 4 seconds.
I want the new activity to load only once.
How can I achieve this?
try this, from enter link description here
in your Activity
#Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
I am trying to make feature that will display text on screen and after few sec to disappear. I have managed that with Handler and Timer classes.
The problem is that I need to somehow stop executing these timers if user makes input over keyboard before timer's time pass and rerun timer again to display different data.
I am facing with problem that the view has remaining visible after user input and disappears after 1-2 sec instead after 5 sec.
the codes that I have used:
//do something
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
}
AND
//do something
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 5 seconds
//do something again
}
}, 5000);
Can you help me to solve this problem?
THanks
For timer you can use timer.cancel(); and for handler you can use handler.removeCallbacks(runnable);
So just declare a Runnable runnable as a global variable, then instantiate it as below
handler.postDelayed(runnable = new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
this is the runnable you will pass when you removeCallbacks. Similarly you can use this handler.removeCallbacks(null); this will stop all the handlers that have been declared. I would suggest that you declare both the handler and the timer as global variables and only instatntiate them when you are calling the timer tasks.
In my RecyclerView there are default animations that play when you remove, add, or change items.
However, performing additional alterations to the items is annoying, because sometimes it doesn't quite update in time if it still thinks the RecyclerView is in the middle of an animation.
So I wanted to have it wait for a bit and then call a final update to the adapter to really make sure those changes are made.
I tried this:
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
refreshAdapter();
}
});
}
}).run();
However for some reason the sleep command still seems to be occupying the main thread! I launch this after pressing OK on a DialogFragment and it just hangs there for a second before it moves forward with anything, and by the time it does, it doesn't do any of the animations and jumps straight to the final result.
How do I fix this?
The issue is you're calling run() on your Thread and not start().
Alternatively, you could use Handler's postDelayed(Runnable, long) from the main Thread to achieve what you're trying to do in less code:
new Handler()
.postDelayed(new Runnable(){
public void run(){
refreshAdapters();
}
}, 1000);
Or
new Handler()
.postDelayed(() -> refreshAdapter(),
1000);
I have two activities in my app, first activity leads to second and the second leads back to the first. In the second one he can record and message and leave his name. In case the user only uses the second activity and just walks away I want that it jumps automatically to the first one after 20 seconds. Do I have to use a Handler for that? Because if I use
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
// my code
}
};
handler.postDelayed(r,20000);
}
});
It will change the activity independently from the user input? Any ideas? Thank you
You need to check if the user left his name in run(). If there is a name, do not execute the code that starts the other activity.
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
if (textViewName.getText().toString().length() == 0) {
// my code
}
}
};
handler.postDelayed(r,20000);
}
});
I have a situation in an Android app where I want to start a network activity (sending out some data) which should run every second. I achieve this as follows:
In the onCreate() I have the code:
tv = new TextView(this);
tv.postDelayed(sendData, 1000);
The sendData() function:
Handler handler = new Handler();
private Runnable sendData=new Runnable(){
public void run(){
try {
//prepare and send the data here..
handler.removeCallbacks(sendData);
handler.postDelayed(sendData, 1000);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
The problem come in like this: When user presses the back buttons and app comes out (UI disappears) the sendData() function still gets executed which is what I want. Now when user re-starts the app, my onCreate() gets called again and I get sendData() invoked twice a second. It goes on like that. Every time user comes out and starts again, one more sendData() per second happens.
What am I doing wrong? Is it my new Handler() creating problem? What is the best way to handle this? I want one sendData() call per second until user quits the app (form application manager).
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
Toast.makeText(c, "check", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 2000);
}
}, 1500);
Perhaps involve the activity's life-cycle methods to achieve this:
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(sendData);
}
#Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(sendData);
}
private final Runnable sendData = new Runnable(){
public void run(){
try {
//prepare and send the data here..
handler.postDelayed(this, 1000);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
In this approach, if you press back-key on your activity or call finish();, it will also stop the postDelayed callings.
Why don't you create service and put logic in onCreate(). In this case even if you press back button service will keep on executing. and once you enter into application it will not call
onCreate() again. Rather it will call onStart()
You can simplify the code like this.
In Java:
new Handler().postDelayed (() -> {
//your code here
}, 1000);
In Kotlin:
Handler().postDelayed({
//your code here
}, 1000)
Please check the below its working on my side in below code your handler will run after every 1 Second when you are on same activity
HandlerThread handlerThread = new HandlerThread("HandlerThread");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
runnable = new Runnable()
{
#Override
public void run()
{
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(runnable, 1000);
Handler h = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==0){
// do stuff
h.removeMessages(0); // clear the handler for those messages with what = 0
h.sendEmptyMessageDelayed(0, 2000);
}
}
};
h.sendEmptyMessage(0);
I think you could experiment with different activity flags, as it sounds like multiple instances.
"singleTop"
"singleTask"
"singleInstance"
Are the ones I would try, they can be defined inside the manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html
You should set andrid:allowRetainTaskState="true" to Launch Activity in Manifest.xml. If this Activty is not Launch Activity. you should set android:launchMode="singleTask" at this activity