I have a activity called "OtpVerification" containing timer of 30 second and during this process if I exit from app and then reopen app it creates problem 1)after few seconds when I reopen app then app starts from MainActivity instead of that activity and 2) app crashes.
here is scenario:-Scenario: A 30 seconds timer starts when Automatic OTP verification process starts, now a user tries to end this process and exit the application by pressing back button, he then immediately relaunches the app within few seconds. Now as the timer expires and the application tries to launch the manual OTP verification screen, this creates a fatal exception and application gets crashed.
here is code:-
@SuppressLint("SetTextI18n")
private void init() {// initialize view controls
//Initialize a new CountDownTimer instance
long m_MillisInFuture = 30000;// timer value
long m_CountDownInterval = 1000;// timer break up
m_oTimer = new CountDownTimer(m_MillisInFuture, m_CountDownInterval) {
public void onTick(long millisUntilFinished) {
@SuppressWarnings("UnusedAssignment") long millis = millisUntilFinished;
@SuppressLint("DefaultLocale") String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
System.out.println(hms);
tv.setText(hms);
//Another one second passed
//Each second ProgressBar progress counter added one
m_n_ProgressStatus += 1;
m_timerProgress.setProgress(m_n_ProgressStatus);
}
public void onFinish() {// when timer finished
/*This is new change ....check if app is in backround or foreground*/
/*if app is in background*/
if (NetworkUtil.isAppIsInBackground(getApplicationContext())) {
System.out.println("In Background");
/*if app is in background start service */
Intent startService = new Intent(getApplicationContext(), OTPIntentService.class); // if condition match then start service to notify server regarding app installation
getApplicationContext().startService(startService);// start service
} else {
System.out.println("In Foreground");
/*if app is in forground than send request to server*/
verifyOTP();
}
}
}.start();// start timer
// retreive progress bar count........
int progressBarMaximumValue = (int) (m_MillisInFuture / m_CountDownInterval);
//Set ProgressBar maximum value
//ProgressBar range (0 to maximum value)
m_timerProgress.setMax(progressBarMaximumValue);
//Display the CountDownTimer initial value
tv.setText(progressBarMaximumValue + "Seconds...");
}
@Override
public void onDestroy() {// unregister broadcast receiver ........
super.onDestroy();
getApplicationContext().unregisterReceiver(m_oOtpReceiver);// unregistaer broadcast receiver.
}