title: Transaction causing activity to recall Firebase Android
body: <p>When I click my <strong>rate_btn</strong> to start this transaction function. </p>
  
  <p>It works fine but in the process it re-run my class activity one more time (my class activity re-runs every time transaction is used) therefore resetting everything like my <strong>msg_ID</strong>. </p>
  
  <p>For example, my <strong>msg_ID</strong> changes (due to using random) every time this class activity is called, so when I run transaction it works but in return it ran the class activity also therefore changing my <strong>msg_ID</strong> aswell.</p>
  
  <p>Here's a scenario: </p>
  
  <blockquote>
    <p>so when click this "rate" button it does the rating
    on the current <strong>msg_ID</strong> but when I click on it again, it rates a different
    <strong>msg_ID</strong> because of my get random <strong>msg_ID</strong>. I think this is caused when
    I call the onComplete method.</p>
  </blockquote>
  
  <p><em>When I click the <strong>rate_btn</em></strong></p>
  
  <p><img src="https://i.stack.imgur.com/mSLIN.png" alt="vote up the first key"></p>
  
  <p><em>Now the class re-run and I click on the <strong>rate_btn</strong> again.</em></p>
  
  <p><img src="https://i.stack.imgur.com/SBFwW.png" alt="vote up second key"></p>
  
  <p>It votes up the first <strong>msg_id</strong> then when I click the <strong>rate_btn</strong> again it votes up the second key (Because of it re-called class activity and the <strong>msg_id</strong> changed)</p>
  
  <p>Here's the code for transaction:</p>
  
  <pre><code>rate_btn.setOnClickListener(new OnClickListener(){
  public void onClick(View v){
          Firebase upvoteref = new Firebase("https://myapp.firebaseio.com/"+msgID+"/upvotes"); 
  
          upvoteref.runTransaction(new Transaction.Handler() {
              @Override
              public Transaction.Result doTransaction(final MutableData currentData) {
                  if (currentData.getValue() == null) {
                      currentData.setValue(1);
                  } else {
                      currentData.setValue((Long) currentData.getValue() + 1);
                  }
  
                  return Transaction.success(currentData);
              }
  
              public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
                  if (firebaseError != null) {
                      System.out.println("Firebase counter increment failed.");
                  } else {
                      System.out.println("Firebase counter increment succeeded.");
                  }
              }
          });
      }
  </code></pre>
  
  <p>Here is the code for retrieving the random id:</p>
  
  <pre><code>    String msgID; //this variable is declare at the start of the class as a global variable.
  
      Firebase ref = new Firebase("https://myapp.firebaseio.com/"+msgID); 
  
      ref.addValueEventListener(new ValueEventListener() {
  
          public void onDataChange(DataSnapshot snapshot) {
              Iterable&lt;DataSnapshot&gt; ds = snapshot.getChildren();
  
              //getting maximum number of children
              long allNum = snapshot.getChildrenCount();
              int maxNum = (int)allNum;
  
              //getting the random integer from number of children
              int randomNum = new Random().nextInt(maxNum);
  
              Iterator&lt;DataSnapshot&gt; ids = ds.iterator();
  
              int count = 0;
  
              //has next will check if there are values in the next iteration , while count is used as a position substitute.
              while(ids.hasNext() &amp;&amp; count &lt; randomNum) {
                  ids.next();
                  count ++; // used as positioning.
              }           
  
              Map&lt;String, Object&gt; newPost = (Map&lt;String, Object&gt;) ids.next().getValue(); // ids will take the value in the iterator (iterate at key) 
              //getting the message from the key
              msgID = newPost.get("id").toString();
          }
  </code></pre>
  
  <p>Any ideas?</p>