Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hector Romero
6,151 PointsBest way to execute a task every given time?
Hi!
I'm working on an tabbed application that shows the reading of some sensors so I need to update the display (one tab) every 15 seconds. I'm doing it with a handler but it doesn't work properly: it's created some times when the tab is shown again, starts even after a login activity is executed, etc. I tried to create it in onCreateView() and in onResume() (since I'm using fragments for the tabs) but I don't get it working.
Can you help me out?
Thanks!
This is my current code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_estado, container, false);
mListView = (ListView) rootView.findViewById(android.R.id.list);
mEmptyTextView = (TextView) rootView.findViewById(android.R.id.empty);
final Handler handler = new Handler();
final int delay = 15000;
handler.postDelayed(new Runnable(){
public void run(){
getMeasure();
handler.postDelayed(this, delay);
}
}, delay);
return rootView;
}
1 Answer
Luiz Carlos
15,714 PointsYour fragment goes be destroyed and created when have interaction with the tabs. Do your task in your activity that have tabs and call the method in your fragment passing the parameters that you need.
Attention: check that your current tab represent the fragment that you need call the method to pass the parameters.