I'm new on android development.
I've develop a ViewPager to show a different image when user click on next button.
But I want to start a CountDownTimer when the new image is displayed.
How can I do this?
My code is for the moment :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int imgNumber = mPageNumber + 1;
String strImg = "ss0" + imgNumber;
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_ssstretch4_split, container, false);
// Set the title view to show the page number.
((TextView) rootView.findViewById(R.id.text1)).setText(
getString(R.string.title_activity_ssstretch4_split, mPageNumber + 1));
//Mettre l'image
int idImg = getResources().getIdentifier("com.trainwithcoachminiil.app:drawable/" + strImg, null, null);
((ImageView) rootView.findViewById(R.id.imgSS)).setImageResource(idImg);
//Lancer le timer
textActivity = (TextView) rootView.findViewById(R.id.textActivity);
textCountDown = (TextView) rootView.findViewById(R.id.textCountDown);
countDownTimerPrepare = new CountDownTimerActivityPrepare(startTimePrepare, interval);
textActivity.setText(R.string.activityPrepare);
textCountDown.setText(String.valueOf(startTimePrepare/1000));
countDownTimerPrepare.start();
return rootView;
}
But when I go to the next page my countdowntimer still running so I get strange behavior.
Thanks a lot if you could tell me how to do.
在Fragment
onDestroy
上调用yourCountDownTimer.cancel()
Viewpager loads atleast two
pages/Fragment
at a time. And it will start your second Fragment/page count timer .
So you should start timer when your fragment is visible
. First handle fragment Visibility using setUserVisible
or setMenuVisibility
delegate's of Fragment. then start your countDown Timer. and cancel timer in on DestroyView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.