title: Android, activity remembering counting data
body: <p>Is there any way to activity remebers the counting, because when you start the app, you click the button which random generates an image from drawable, and it doesn't let user to press that button for 30 secs.. the problem now is that when you are waiting for counter go to 0 you can easily press option menu, click settings and click "povratak" ,which starts activity all over again and the counter losses its point because user can now again press button that generates image (and I dont want that):/</p>
  
  <p>MainActivity</p>
  
  <pre><code>    package com.example.ams;
  
  import java.util.Random;
  
  
  
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  
  
  
  public class MainActivity extends Activity {
  
      final Random rnd = new Random();
      ImageView img = null;
      Button btnRandom = null;
  
  
  
  
  
  
      @Override
      protected void onCreate(
          final Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
          img = (ImageView) findViewById(R.id.imgRandom);
          btnRandom = (Button) findViewById(R.id.btnRandom);
      }
  
  
  
      protected final static int getResourceID
      (final String resName, final String resType, final Context ctx)
      {
          final int ResourceID =
              ctx.getResources().getIdentifier(resName, resType,
                  ctx.getApplicationInfo().packageName);
          if (ResourceID == 0)
          {
              throw new IllegalArgumentException
              (
                  "No resource string found with name " + resName
                  );
          }
          else
          {
              return ResourceID;
          }
      }
  
      public void clickHandler(final View v)
      {
          switch(v.getId())
          {
  
  
  
          case R.id.btnRandom:
              {
                  if (!btnRandom.isEnabled())
                  {
                      return;
                  }
  
                  // I have 3 images named img_0 to img_2, so...
                  final String str = "img_" + rnd.nextInt(45);
                  img.setImageDrawable
                  (
                      getResources().getDrawable(getResourceID(str, "drawable",
                          getApplicationContext()))
                  );
                  btnRandom.setEnabled(false);
  
                  new CountDownTimer(30000, 1000) // Wait 30 secs, tick every 1 sec
                  {
                      @Override
                      public final void onTick(final long millisUntilFinished)
                      {
                          btnRandom.setText("Pričekaj do sljedeće kartice: " + (millisUntilFinished / 1000));
                      }
                      @Override
                      public final void onFinish()
                      {
                          btnRandom.setText("PROMIJENI KARTICU !");
                          btnRandom.setEnabled(true);
                      }
                  }.start();
  
                  break;
              }
  
  
              }
  
          }
  
  
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle item selection
          switch (item.getItemId()) {
              case R.id.action_settings:
                  setContentView(R.layout.help);
                  return true;
                      default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater=getMenuInflater();
          inflater.inflate(R.menu.main, menu);
          return super.onCreateOptionsMenu(menu);
  
      }
  
      public void goBack(View v){
          startActivity(new Intent(this, MainActivity.class));
          }
  
      @Override
      public void onBackPressed() {
      }
  
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity"
  android:background="@drawable/bgi"
  &gt;
  &lt;ImageView
      android:id="@+id/imgRandom"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
  /&gt;
  
  &lt;Button
      android:id="@+id/btnRandom"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/imgRandom"
      android:layout_alignParentBottom="true"
      android:layout_alignRight="@+id/imgRandom"
      android:onClick="clickHandler"
      android:text=" Promijeni karticu !"
      android:textColor="#ffffff"
      android:textSize="25dp" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>help.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" &gt;
  
  &lt;ScrollView
      android:id="@+id/SCROLLER_ID"
      android:layout_width="fill_parent"
      android:layout_height="450dp"
      android:fillViewport="true"
      android:scrollbars="vertical" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="true"
      android:enabled="true"
      android:freezesText="false"
      android:overScrollMode="always"
      android:scrollbarAlwaysDrawVerticalTrack="true"
      android:scrollbarStyle="insideOverlay"
      android:scrollbars="vertical"
      android:text="UVOD:Uz svaki scenario organizator moze odrediti da se koristi &amp;quot;AMS sustav&amp;quot; zbog realnijeg pristupa igri i viseg stupnja MILSIM-a. Organizator bira medice (ili kako se vec odredi) i oni moraju imati prilikom pocetka igre 46 kartica. /&gt;
  
  
  &lt;/ScrollView&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="320dp"
      android:layout_height="wrap_content"
      android:text="Povratak" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>And if there is possible , show me your answers in code format, cause english is not my native language and often I don't understand what you guys mean :)</p>