There is a program that increments the value of a variable every second, displays it in a TextView and makes a sound:

 public class MainActivity extends ActionBarActivity{ int time=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } public void start(View v){ TextView txt = (TextView)findViewById(R.id.textView1); myTimer = new Timer(); final Handler uiHandler = new Handler(); myTimer.schedule(new TimerTask() { @Override public void run() { uiHandler.post(new Runnable() { @Override public void run() { time = time + 1; txt.setText( String.valueOf(time)); } }); } }, 1000, 10L * 100); } } 

When the screen is locked, the program continues to make a sound, but when unlocked, the Activity recreated, and in TextView empty. Is it possible to forbid recreating an Activity ?

  • To get started, read the documentation. Start with onSaveInstanceState . - falstaf

2 answers 2

The value of a variable is increased in the service, for example, then it will not be reset.

And if no joke, then onSaveInstanceState and onRestoreInstanceState will allow you to save and restore the value of a variable when you destroy an Activity.

  • Preserve the value until the screen lock and, accordingly, everything that will be added during the locked screen will not be taken into account - tpmanc
  • @tpmanc when you lock the screen, you can save the value of a variable in SharedPreferences and start the service, in the service update the variable in SharedPreferences. When the Activity starts up again in onResume, read this variable. - Suvitruf
  • But this is quite a difficult implementation of the easy task, you see. - Helisia
  • @SuperCreeper to turn the thread, which will increase the counter, even when the application is minimized, only in the service it turns out. - Suvitruf
  • @Suvitruf, I would say that the service also will not work. the system kills services as well as applications, but not so quickly). priorities are different)) but still it all depends on the configuration and on the version of the system and on the phase of the moon too. Here, as a human being, AlarmReceiver should be gentlemen !!! And do not keep anything in memory - flush on the file, save to preferences - yes, do anything but not in memory. - SATALIN

You have because of the Timer variable increase will not go on forever, if that. Try this:

 Handler h = new Handler(); public void start(View v) { TextView text = ...; h.post(update); //первый раз запускаем без задержки... } Runnable update = new Runnable() { @Override public void run() { time++; text.setText("" + time); h.postDelayed(update, 1000); //а все последующие разы с задержкой в 1с } }; @Override public void onPause() { h.removeCallbacks(update); //перестаём обновлять super.onPause(); } @Override public void onResume() { h.post(update); //при возвращении снова обновляемся, если требуется super.onResume(); } 
  • Why will not increase forever, if you do not block the screen, then it increases until I close the application - tpmanc