-5

I have a text written in my app. I want it to be changed after some time automatically after the app starts. I need to invoke a method somewhere so that it can be called automatically. I've tried putting my code in onStart() method, but that delays the starting of my app, that's why I want my method to be invoked after onStart() method.

In other words, I want my app to get started, see the previous text and after waiting for some fixed time (I will use Thread.sleep(ms)) it would change to some other text.

I need to submit a project. Please help me with the problem. Thank you.

Onik
  • 19,396
  • 14
  • 68
  • 91
  • 1
    Check Android documentation on Activities: http://developer.android.com/training/basics/activity-lifecycle/starting.html – Tautvydas Dec 28 '14 at 12:45

1 Answers1

0

For the purpose of app responsiveness, as stated in the official documentation on Processes and Threads, it is not recommended to block the UI-thread (for example by calling Thread.sleep()) longer than a few seconds. As also described in the article provided there are several options to avoid that, one of which is to use Handler.postDelayed(Runnable, int) method with the timeout indicating when the new text should show up:

private static final int TIME_OUT = 1000; // [ms]
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    tv = (TextView) findViewById(R.id.yourTextViewId);
    tv.setText("This is the text to appear when Activity starts up");

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            tv.setText("New text to be shown after TIME_OUT");

        }
    }, TIME_OUT);
}
Onik
  • 19,396
  • 14
  • 68
  • 91