2

I want a new activity to start when a new activity is reached, so far i've been having some trouble. This is what i have so far. I hope you guys understand what i am trying to do. If there is an easier way that works please tell me. perhaps i should put this in a thread?

 Chrono = (Chronometer) findViewById(R.id.chronometer1);
        Chrono.start();
        while (!(Chrono.equals(chronoText))) {
            chronoText = Chrono.getText().toString();
            if (chronoText.equals("00:30")) {
                Intent intent = new Intent(getApplicationContext(),
                        Hw3Activity.class);
                startActivity(intent);
            }

        }

My whole code:

    public class Next extends Activity {
        Button returned;
        Button click;
        Button search;
        EditText clicksearch;
        TextView counted;
        int counter;
        int answer;
        Chronometer Chrono;
        CharSequence chronoText;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screentwo);
            // waiter = new Waiter(60 * 1000); // 1 min
            // waiter.start();

            Chrono = (Chronometer) findViewById(R.id.chronometer1);
            Chrono.start();
            Log.d("Ebz", "uhh");
            returned = (Button) findViewById(R.id.returned);
            click = (Button) findViewById(R.id.click);
            search = (Button) findViewById(R.id.search);
            clicksearch = (EditText) findViewById(R.id.clicksearch);
            counted = (TextView) findViewById(R.id.counted);
    chronometer(); // its called here
returned.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    counter = 0;
                    Intent intent = new Intent(getBaseContext(), Hw3Activity.class);
                    answer = intent.getIntExtra("meaningOfLife", -1);

                    Intent i = getIntent();
                    i.putExtra("returnInt", answer);
                    setResult(RESULT_OK, i);
                    startActivity(intent);

                }
            });
            click.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    counter++;
                    counted.setText(String.valueOf(counter));
                    Chrono.setBase(SystemClock.elapsedRealtime());

                }
            });
            search.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                    intent.putExtra(SearchManager.QUERY, clicksearch.getText()
                            .toString());
                    startActivity(intent);
                }
            });
            clicksearch.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                }
            });
        }

        public void chronometer() {
            while (!(Chrono.equals(chronoText))) {
                chronoText = Chrono.getText();
                Log.d("Ebz", "makes it to chronometer");
                counted.setText(String.valueOf(chronoText));
                if (chronoText.equals("00:10")) {
                    Intent intent = new Intent(getApplicationContext(),
                            Hw3Activity.class);
                    startActivity(intent);
                }

            }
        }
    }
The Tokenizer
  • 1,564
  • 3
  • 29
  • 46

1 Answers1

7

Try implementing Chronometer.OnChronometerTickListener on your Activity and put the code from chronometer() in there (or the relevant part).

public class Next extends Activity
    implements Chronometer.OnChronometerTickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...

        Chrono = (Chronometer) findViewById(R.id.chronometer1);
        Chrono.setOnChronometerTickListener(this);
        Chrono.start();

        ...
    }

    public void onChronometerTick(Chronometer chronometer) {
        if ("00:10".equals(chronometer.getText()) {
            // Create Intent and start the new Activity here
        }
    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • This method does seem cleaner then the way i was doing it, i am going to try it later tonight and accept answer if it works, Thank you ill be back soon. – The Tokenizer Feb 06 '12 at 00:04
  • why do you pass 'this' as a parameter in `Chrono.setOnChronometerTickListener()`? i'm quite new to android. – Sam Oct 28 '15 at 15:18
  • When a Java class implements an interface, it must implement the various methods of the interface. In this case the class is called `Next` (which in itself extends `Activity`) and the interface is `Chronometer.OnChronometerTickListener`. That interface defines a method called `onChronometerTick` which is what is called each time the `Chronometer` 'ticks'. This is implemented by `Next` as you can see in the code above.....CONTINUED IN FOLLOWING COMMENT... – Squonk Nov 01 '15 at 13:12
  • In order for the `Chronometer` to 'know' how to find that method we call the `setOnChronometerTickListener` method and pass it an instance of a class which implements the `onChronometerTick` method. In this case, the instance is the `Activity` itself and the Java keyword `this` refers to the current instance of whatever object is making the call to `setOnChronometerTickListener`. This isn't Android-specific, it's a Java thing - Google for 'Java this keyword' and read up on it - you'll be using it a lot. – Squonk Nov 01 '15 at 13:16