1

I have created a class that is extending from CountDownTimer, It has a a method onFinish() which calls when timer expires.

There are 6 Activities, user can be in any activity when timer expires, So in CounterTimerwhen Finish() method calls , i need to show an Alert Message to the user,along with i need to redirect user to Login page.

Things getting confusing, as i cannot call Intent class in the Normal Class, I can also not pass the context, as user can be in any activity.

I have written following code, but its not helping out.

I am using context here, but its giving error message on passing context to Intent

public  class CounterClass extends CountDownTimer implements ITMServiceEvent {

  @Override
    public void onFinish() {
        if(sql_code.equalsIgnoreCase("0")) {
            String resultCode = command1.getString("result");
            context.startActivity(context.getApplicationContext(), MainActivity.class);
            }

Calling Timer at the Start of Wizard, in a Fragment

CounterClass counterClass= new CounterClass(180000,1000);
counterClass.setTextView(tvTimer);
counterClass.start();
dev90
  • 7,187
  • 15
  • 80
  • 153

1 Answers1

1

There are two parts of your question, first is how you can clean up the Activity stack and start a new Activity on Top of them, I suppose this would be the LoginActivity in your case.

To do this, you need to set the Flag of your LoginActivity Intent when you want to start it,

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

And the second part is, you want to be able to finish the current activity after showing a dialog to the user. I assume your Timer is a Service Class, which runs in the background. The way to tell your current activity that the Time is Up ! is to send a Broadcast Message. Preferably, LocalBroadcastManager can help you out. You can have a BaseActivity class where all of your 6 Activities can be extended from it and you can register/unregister LocalBroadcastManager to/from those activities in the BaseActivity class (register in onResume and unregister in onPause). After you register them you just need to implement and handle the onReceive method where you can show a dialog and start the LoginActivity after finishing the current one.

osayilgan
  • 5,873
  • 7
  • 47
  • 68