Alert dialog does not appear

672 views Asked by At

I have an application in which when the user starts the application a timer starts. After 10sec an AlertDialog pops up saying only 15 seconds reaming and displays a timer, and after 14 seconds it disappears. This works fine when on the first activity of the application. If the user passes from first Activty --> TimedNotify Activity the timer stops after 10seconds. onUserInteraction() in TimedNotify the timer restarts and works absolutely fine. Please assist me as to where I am going wrong.

public class FirstActivity extends TimedNotify{
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {    
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.timercheck);    
        final Button btnstart2 = (Button) findViewById(R.id.btn);

        btnstart2.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this,
                                       TimedNotify.class);
                 startActivity(intent);
            }
        });
    }
}

public class TimedAlert extends Activity 
{
    static CountDownTimer timer1, timer2;
    int flag = 0;
    protected static final String TAG = null;
    public static AlertDialog alert, alertdialog;
    private static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView mCounter1TextField = (TextView) findViewById (R.id.mCounter1TextField);

        // first timer set for 10sec
        timer1 = new CountDownTimer(10000, 1000) 
        {
            @Override
            public void onTick(long millisUntilFinished) 
            {
                Log.v(TAG, "timer1 ticking");
                mCounter1TextField.setText("Seconds left: "
                                            + formatTime(millisUntilFinished));
            }

            public void onFinish() {
                //after 10sec display alert box and show timer 
                Log.v(TAG, "timer1 finished");
                timer1.cancel();
                AlertDialog.Builder builder = new AlertDialog.Builder(
                                    TimedAlert.this);
                builder.setTitle("Session Time Out");
                builder.setMessage("00:15");
                builder.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog2,int iwhich)
                    {
                        Intent in = new Intent(TimedAlert.this,FirstActivity.class);
                        //in case there are many events ..the intent should be passed  to the last activity on clicking resume
                        in.setAction(Intent.ACTION_MAIN);
                        in.addCategory(Intent.CATEGORY_LAUNCHER);
                        onUserInteraction();
                    }
                });

                builder.setNegativeButton ("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog2,int iwhich)
                    {
                        timer2.cancel();
                        timer1.start();
                    }
                });

                alert = builder.create();
                alert.show();

                timer2 = new CountDownTimer(15000, 1000) 
                {
                    @Override
                    public void onTick(long millisUntilFinished)
                    {
                        Log.v(TAG, "timer2 ticking");
                        alert.setMessage("Your Session will expire in 5 minutes . Timleft00:"+  (millisUntilFinished / 1000));
                        mCounter1TextField.setText("Seconds left: "+  formatTime (millisUntilFinished));
                    }

                    //after 15 sec dismiss alert box
                    public void onFinish() {
                        Log.v(TAG, "timer2 finished");
                        timer2.cancel();
                        alert.dismiss();
                    }
                }.start();
            }
        }.start();
    }

    @Override
    public void onBackPressed() {
        Intent in = new Intent(TimedAlert.this, FirstActivity.class);
        startActivity(in);
    }

    public String formatTime(long millis) {
        String output = "00:00";
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        seconds = seconds % 60;
        minutes = minutes % 60;
        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);

        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
        output = minutesD + " : " + secondsD;
        return output;
    }

    public void onUserInteraction() {
        super.onUserInteraction();
        // Remove any previous callback
        try {
            Log.v(TAG, "user interacted");
            timer1.start();
            timer2.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.v(TAG, "paused");
        onUserInteraction();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        Log.v(TAG, "resumed");
        onUserInteraction();
    }

    private void handleIntent(Intent intent) {
        timer1.start();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

        Log.v(TAG, "stopped");
        timer1.start();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.v(TAG, "started");
        timer1.start();
    }
}
1

There are 1 answers

0
Ken Y-N On

OK, here are a few things I noted that might help you out:

public void onClick(DialogInterface dialog2, int iwhich) {
    Intent in = new Intent(TimedAlert.this,
            FirstActivity.class);
    in.setAction(Intent.ACTION_MAIN);
    in.addCategory(Intent.CATEGORY_LAUNCHER);

    onUserInteraction();
}

You don't have a startActivity(in); after setting up all the parameters.

Why do onPause() and onResume() call onUserInteraction(), but onStart() and onStop() don't?

In fact, you should choose whether to use onPause() and onResume() only or onStart() and onStop(). Furthermore, onPause() or onStop() shouldn't restart the timers?

Thinking further about your reported problem, you say that it is when you are on your second activity that you have problems. Check out the lifecycle of an Activity - I suspect what might be happening is that you launch a new instance of your activity. Try setting your manifest to use android:launch mode="singleTask" for your activity.