0

I am trying to load some data. Id data is loaded in 20 seconds than i start new activity else i will finish by giving some relevant message. I have started a countdownTimer to keep track of time. Once data is loaded, I want to stop the timer. I have Following class :

public class SplashActivity extends AppCompatActivity {

    private Context mContext;
    private Boolean mDataLoadedFromServer = false;
    private String mJSONData;
    private SplashTimerForLoadingMasterDataForAllChannels mTimer;

    private void stopTimer(){
        mTimer.cancel();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_splash);
        mTimer = new SplashTimerForLoadingMasterDataForAllChannels(20000,1000);
        mTimer.start();
    }



        class SplashTimerForLoadingMasterDataForAllChannels extends CountDownTimer {

        public SplashTimerForLoadingMasterDataForAllChannels(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            Log.d("testTimer", "SplashTimerForLoadingMasterDataForAllChannels");
            //SomE AsyncTAsk
            LoadData loaddata = new LoadData();
            loaddata.execute();
            //SomE AsyncTAsk
        }
        @Override
        public void onTick(long millisUntilFinished) {
            Log.d("testTimer", "onTick millisUntilFinished = " + millisUntilFinished + " mDataLoadedFromServer = " + mDataLoadedFromServer);
            //mDataLoadedFromServer is modified once Data is loaded in AsyncTask
            if(mDataLoadedFromServer) {
                stopTimer();
            }
        }

        @Override
        public void onFinish() {
            Log.d("testTimer", "onFinish");
            if(mDataLoadedFromServer) {
                mDataSavedAndNextActivityLaunched = true;
                if (Utils.checkIfUserLoggedIn()) {
                    mContext.startActivity(new Intent(mContext, ABCACtivity.class));
                } else {
                    mContext.startActivity(new Intent(mContext, XYZActivity.class));
                }
                finish();
            }
        }

    }



}

I cancel it in a local method call but onTick still keeps getting called. Can someone please help?

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
  • I've never written a code like this, but... I think you can all actions write in `onTick()` method. Let's say all data are ALWAYS loaded in 20 seconds. So if `Tick()` returns you 20 second, permanently run a new `ABCActivity`. Try this at first. – piotrek1543 Dec 16 '15 at 19:26
  • If it's working fine, I suppose that you have also a method that checks if Data is loaded. So in your `if` statement in onTick() write a statement which chcecks not only if data was loaded in 20 seconds, but also if REALLY data was loaded in that time. Otherwise (`else` statement) run `XYZActivity` – piotrek1543 Dec 16 '15 at 19:29
  • Your onTick() is still getting called because you missed to remove callbacks. Moreover, I don't see what exactly `stopTimer()` is doing. You put there your code partially so it looks so unclean, that many guys don't know how to help you. If you don't want to put whole code. Try a method above. Hope it help – piotrek1543 Dec 16 '15 at 19:31
  • @piotrek1543 StopTimer is mentioned at start it is : private void stopTimer(){ mTimer.cancel(); } It just cancels the timer. Also what callbacks and how i can clear them? also For Second comment.. i Can check same in OnTick() but Even after XYZActivity is launched, onTick Keeps getting called and i can see its Logs. How to Stop it? LEt us say my data gets loaded in 2 seconds and hence i need to stop tiimer than and there. – Tarun Deep Attri Dec 17 '15 at 06:33
  • There are some issues on StackOverflow like http://stackoverflow.com/questions/3138348/how-to-stop-cancel-android-countdowntime. In one of them I read that CountDownTimer works well if you call it outside of method, not into it. – piotrek1543 Dec 17 '15 at 07:14
  • Yes thats why i created a local method stopTimer() from which i am calling mTimer.cancel(); But still OnTick Keeps getting called...:-( – Tarun Deep Attri Dec 17 '15 at 08:56

3 Answers3

1

Basically counter does not simply stop if I cancle it from onTick() or from onFinish() ie FROM INSIDE TIMER.

However it stops very easily if I do it from any point outside of timer.

So the point at which my data is fully loaded...I called timer.cancle() and it did the trick.

However I still dont understand why it does not work if we do same from inside timer methods.

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
1

I tried this code snippet, since most answers are saying you cannot cancel the timer inside its implementation, thus i tried using a handler inside onFinish. Old post but if anyone comes across this its helpful.

new Handler().post(new Runnable() {
        @Override
        public void run() {
            timerTextView.setText("00:" + String.format("%02d", counter));
            cancel();
        }
    });
0

you need to call stopTimer() outside of the CountDownTimer's onTick(),

something like this

@Override
    public void onFinish() {
        Log.d("testTimer", "onFinish");
        if(mDataLoadedFromServer) {
            mDataSavedAndNextActivityLaunched = true;
            if (Utils.checkIfUserLoggedIn()) {
                mContext.startActivity(new Intent(mContext, ABCACtivity.class));
            } else {
                mContext.startActivity(new Intent(mContext, XYZActivity.class));
            }
            finish();
            stopTimer();
        }
    }
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • But i dont want to call stopTimer() onFinish of counter....As and when data is loaded ....ASAP i need to cancle timer...So i check for data loaded in every onTick and if data is loaded than not wait for finish and stop immediately. – Tarun Deep Attri May 04 '16 at 04:51
  • it won't work in `onTick()`, what else you can do is try [Timer](http://developer.android.com/intl/es/reference/java/util/Timer.html) instead. – Atiq May 04 '16 at 06:51