0

I've got a Chronometer, but when I change my View (with ActionBar) it stops.

I don't really understand how to use AsyncTask, and I didn't find a Tutorial.

Is there something easy to solve my problem? I would really like an example with Chronometer...

Thanks very much!

Code so far:

public class BFragment extends SherlockFragment {

private Button start;
private View v;
private Chronometer chrono;
private Button pause;
private Button reset;
private long lastPause;
private Button resume;
private Boolean richtig;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    v = inflater.inflate(R.layout.activity_bfragment, container, false);
    start = (Button) v.findViewById(R.id.start); 
    chrono = (Chronometer) v.findViewById(R.id.chronometer1);
    pause = (Button) v.findViewById(R.id.pause);
    reset = (Button) v.findViewById(R.id.reset);
    resume = (Button) v.findViewById(R.id.resume);

    richtig = true;

    new chronom().execute();



    return v;

}
private class chronom extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
        start.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                chrono.setBase(SystemClock.elapsedRealtime());
                chrono.start();

                System.out.println(SystemClock.elapsedRealtime());
                richtig = false;
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                lastPause = SystemClock.elapsedRealtime();
                chrono.stop();
                richtig = true;

            }
        });

        resume.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (richtig) {
                    chrono.setBase(chrono.getBase() + SystemClock.elapsedRealtime() - lastPause);
                    chrono.start();
                } else {

                }
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                chrono.setBase(SystemClock.elapsedRealtime());
                System.out.println(chrono.getBase());
                richtig = false;
            }
        });

        return null;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Long result) {

    }
}
}

I just copied the code... (Chrono works, but not in Background)

moritzg
  • 4,266
  • 3
  • 37
  • 62
  • post a sample code so we can find out what's wrong with your async – cyborg86pl Jul 21 '12 at 20:07
  • @cyborg86pl: Here's my code (Edited Question) – moritzg Jul 21 '12 at 20:15
  • ok so it's about `AsyncTask` working in your BFragment class only. to fire chrono with no matter what view You use, there are [`Service`s](http://developer.android.com/reference/android/app/Service.html) that would be better cause they run totally off the application. there also is a thing, that you only set listeners in async, You should do that in main thread – cyborg86pl Jul 21 '12 at 20:37
  • I'm sorry, but I don't understand anything you say... I'm quite new to Android Developing... I would really like to have a working example otherwise i cant code anything... I'm sorry – moritzg Jul 21 '12 at 20:42

0 Answers0