java - How can i stop the CountDowntimer when the user already finish the game -


i want stop timer when activity finish or leave. im trying when user answer jumble words timer stop. , if user didnt answer time , score post on next activity. dont know how it. please me thanks!

anagram.java

package com.example.child.fragments;    import android.app.activity;  import android.app.fragment;  import android.content.intent;  import android.os.bundle;  import android.os.countdowntimer;  import android.support.annotation.nullable;  import android.view.layoutinflater;  import android.view.view;  import android.view.viewgroup;  import android.widget.button;  import android.widget.edittext;  import android.widget.relativelayout;  import android.widget.textview;    import com.example.child.sidenavigation.guessthepic;  import com.example.child.sidenavigation.fade;  import com.plattysoft.leonids.particlesystem;  import com.plattysoft.leonids.modifiers.scalemodifier;    import com.example.child.sidenavigation.r;    import org.w3c.dom.text;    import java.util.arraylist;  import java.util.arrays;  import java.util.collections;  import java.util.list;  import java.util.random;      /**   * created child on 7/4/2017.   */    public class anagram extends activity implements view.onclicklistener {      arraylist<string> dictionary = new arraylist<>();        public int counter;        textview tv_info,tv_word,textview,scores;      edittext et_guess;      button b_check,start,cancel;      relativelayout rel;      random r;      int score = 0;        string currentword;              @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.anagram);          dictionary.add("one");          dictionary.add("two");          dictionary.add("three");          textview = (textview)findviewbyid(r.id.textview);          scores = (textview)findviewbyid(r.id.score);          rel = (relativelayout)findviewbyid(r.id.agram);          start = (button)findviewbyid(r.id.button);          cancel = (button)findviewbyid(r.id.cancel);          tv_info = (textview)findviewbyid(r.id.tv_info);          tv_word = (textview)findviewbyid(r.id.tv_word);          et_guess = (edittext)findviewbyid(r.id.et_guess);          b_check = (button)findviewbyid(r.id.b_check);          b_check.setonclicklistener(this);          r = new random();            newgame();            start.setonclicklistener(new view.onclicklistener() {              @override              public void onclick(view v) {                  new countdowntimer(30000, 1000){                      public void ontick(long millisuntilfinished){                          textview.settext(string.valueof(counter));                          counter++;                          rel.setvisibility(view.visible);                          start.setvisibility(view.gone);                          cancel.setvisibility(view.gone);                        }                      public  void onfinish(){                          textview.settext("finish!!");                          intent intent = new intent();                          intent.setclass(anagram.this, fade.class);                          startactivity(intent);                          overridependingtransition(r.anim.fade_in, r.anim.fade_out);                      }                  }.start();              }          });          }      private string shuffleword(string word) {          list<string> letters = arrays.aslist(word.split(""));          collections.shuffle(letters);          string shuffled = "";          for(string letter:letters) {              shuffled += letter;          }          return shuffled;      }      private void newgame() {          if(dictionary.size() < 1) {              intent f = new intent(this,fade.class);              f.putextra("time",textview.gettext().tostring());              f.putextra("score",scores.gettext().tostring());              startactivity(f);              return;            }            //get random word dictionary          currentword = dictionary.get(r.nextint(dictionary.size()));            //shoow shuffleword          tv_word.settext(shuffleword(currentword));                // clear text          et_guess.settext("");          b_check.setenabled(true);      }          @override      public void onclick(view v) {          string guess = et_guess.gettext().tostring().tolowercase();            if(guess.equals(currentword)) {              dictionary.remove(guess);              tv_info.settext("awesome!");              scores.settext("" + integer.tostring(score));              score = score+1;              b_check.setenabled(false);              newgame();                new particlesystem(this, 10, r.drawable.star, 3000)                      .setspeedbycomponentsrange(-0.1f, 0.1f, -0.1f, 0.02f)                      .setacceleration(0.000003f, 90)                      .setinitialrotationrange(0, 360)                      .setrotationspeed(120)                      .setfadeout(2000)                      .addmodifier(new scalemodifier(0f, 1.5f, 0, 1500))                      .oneshot(v, 10);          } else {              tv_info.settext("try again");          }      }    }

anagram.xml

<?xml version="1.0" encoding="utf-8"?>  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical" android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="@drawable/anagramback">        <textview          android:id="@+id/textview"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textappearance="?android:attr/textappearancelarge"          android:textsize="40sp"          android:layout_alignparenttop="true"          android:layout_centerhorizontal="true" />        <button          android:text="start"          android:layout_width="150dp"          android:layout_height="100dp"          android:id="@+id/button"          android:textappearance="?android:attr/textappearancelarge"          android:textstyle="bold|italic"          android:layout_margintop="71dp"          android:layout_below="@+id/textview"          android:layout_centerhorizontal="true" />        <button          android:text="cancel"          android:layout_width="150dp"          android:layout_height="100dp"          android:id="@+id/cancel"          android:textappearance="?android:attr/textappearancelarge"          android:textstyle="bold|italic"          android:layout_below="@+id/button"          android:layout_alignleft="@+id/button"          android:layout_alignstart="@+id/button" />          <relativelayout          android:layout_width="match_parent"          android:layout_height="match_parent"          android:id="@+id/agram"          android:visibility="gone">          <textview          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/tv_info"          android:layout_marginbottom="32dp"          android:layout_above="@+id/tv_word"          android:layout_centerhorizontal="true" />        <textview          android:text=""          android:textsize="24dp"          android:layout_width="match_parent"          android:gravity="center"          android:layout_height="wrap_content"          android:id="@+id/tv_word"          android:layout_marginbottom="39dp"          android:layout_above="@+id/et_guess"          android:layout_alignparentleft="true"          android:layout_alignparentstart="true" />        <edittext          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:inputtype="textpersonname"          android:text=""          android:ems="10"          android:hint="word"          android:id="@+id/et_guess"          android:layout_above="@+id/b_check"          android:layout_alignparentleft="true"          android:layout_alignparentstart="true" />        <button          android:id="@+id/b_check"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:text="check"          android:layout_marginbottom="219dp"          android:layout_alignparentbottom="true"          android:layout_alignparentleft="true"          android:layout_alignparentstart="true" />        </relativelayout>        <textview          android:id="@+id/score"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@+id/agram"          android:layout_toendof="@+id/agram"          android:layout_torightof="@+id/agram" />      </relativelayout>

fade.java

package com.example.child.sidenavigation;    import android.app.activity;  import android.content.intent;  import android.os.bundle;  import android.view.view;  import android.widget.button;  import android.widget.textview;    import com.example.child.fragments.importfragment;    import org.w3c.dom.text;    /**   * created child on 7/13/2017.   */    public class fade extends activity implements view.onclicklistener {      textview time,score;      button tryagain;      @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.fade);          time = (textview)findviewbyid(r.id.clock);          tryagain = (button)findviewbyid(r.id.tryagain);          score = (textview)findviewbyid(r.id.score);            intent getint = getintent();          string val = (string) getint.getserializableextra("time");          string score = (string)getint.getserializableextra("score");          score.settext(score);          time.settext(val);        }        @override      public void onclick(view v) {          if (v == tryagain) {              intent newgame = new intent(fade.this,importfragment.class);              startactivity(newgame);          }      }  }

fade.xml

<?xml version="1.0" encoding="utf-8"?>  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical" android:layout_width="match_parent"      android:layout_height="match_parent">        <button          android:text="try again"          android:textsize="20dp"          android:id="@+id/tryagain"          android:layout_width="200dp"          android:layout_height="wrap_content"          android:layout_alignparentbottom="true"          android:layout_centerhorizontal="true"          android:layout_marginbottom="149dp" />        <textview          android:id="@+id/clock"          android:text="time"          android:textsize="30dp"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignparenttop="true"          android:layout_centerhorizontal="true" />        <textview          android:id="@+id/score"          android:textsize="30dp"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignleft="@+id/clock"          android:layout_alignstart="@+id/clock"          android:layout_below="@+id/clock"          android:layout_margintop="125dp"          android:text= "score" />      </relativelayout>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -