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
?
onSaveInstanceState
. - falstaf