13

i need to start an activity from the current activity after a certain time period. I coded like below.

public class FirstActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);


    new Timer().schedule(new TimerTask(){
        public void run() { 
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
        }
    }, 2000); 
}

But its not working..it keeps on crashing.. Is my method correct? my manifest file is as below

`

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_first" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.first.FirstActivity" />
    </activity>
</application>

Ram
  • 147
  • 1
  • 1
  • 7
  • what the error logs says? have you added your second activity in your manifest file? – vinothp Nov 05 '12 at 11:18
  • use timer function http://stackoverflow.com/questions/1877417/how-to-set-a-timer-in-android – imlearning Nov 05 '12 at 11:22
  • In error log its not displaying any messages. the application is able to open in the emulator. but it got crashed after that. here is my manifest file. – Ram Nov 05 '12 at 12:22
  • i have tried with all the answers below. But it is still crashing after that time period. – Ram Nov 05 '12 at 13:18
  • Thanks all... its working now.. the problem is with the second activity not with the timer. when i comment out "getActionBar().setDisplayHomeAsUpEnabled(true);" these lines, its working fine. It wont give any error at compile time but, during runtime it will be an issue.Thanks. – Ram Nov 05 '12 at 14:10

9 Answers9

20

You can use the Handler class postDelayed() method to perform this:

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        //start your activity here  
    }

}, 1000L);

Where 1000L is the time in milliseconds after which the code within the Runnable class will be called.

Try to use this .

JDJ
  • 4,298
  • 3
  • 25
  • 44
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
9

use runOnUiThread for starting activity from Timer as:

new Timer().schedule(new TimerTask(){
        public void run() { 
         FirstActivity.this.runOnUiThread(new Runnable() {
            public void run() {
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
          }
        });
       }
    }, 2000);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

Give this a try. I use this for a Splash Screen in my app and works just fine. Also, as @Venture pointed out in the comment, make sure the activity is added to your manifest file.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent startActivity = new Intent(FirstActivity.this, SecondActivity.class);
        startActivity(startActivity);
        finish();
    }
}, 2000);
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
1

Thanks all... its working now.. the problem is with the second activity not with the timer. when i comment out "getActionBar().setDisplayHomeAsUpEnabled(true);" these lines in the second activity, it started working . these lines wont give any error at compile time but, during runtime it will be an issue.Thanks.

Ram
  • 147
  • 1
  • 1
  • 7
1

I found this on the net. You have that create a interface and you implement it in you class in a method anonymous

public static void delay(int timeMillisecond , final DelayCallBack delayCallBack){
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            delayCallBack.postDelay();
        }
    } , timeMillisecond );
}

public interface DelayCallBack{
    void postDelay();
}

Util, That's where I have my static methods

Util.delay( delay , new Util.DelayCallBack() {
    @Override
    public void postDelay() {
        startActivity( new Intent( [currentContext] , [nextActivity] );
    }
});
AdrianCmn
  • 11
  • 2
0

I don't know what is wrong with your code but this should work:

private Handler mHandler = new Handler();
mHandler.postDelayed(mStartActivityTask, 2000);
private Runnable mStartActivityTask= new Runnable() {
    public void run() {
       // Start the Activity
    }
};
iTurki
  • 16,292
  • 20
  • 87
  • 132
0

Can you try this:

Thread toRun = new Thread()

        {
               public void run()
               {
                   try {                      
                    sleep(500);

                     Intent intent = new Intent (FirstActivity.this, SecondActivity.class);
                     startActivity(intent);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               }
        };
        toRun.start();
Lokesh
  • 5,180
  • 4
  • 27
  • 42
0
@Override
protected void onStart() {
    super.onStart();

    try {
    Runnable myRunnable = new Runnable(){

        public void run(){

        try{
           sleep(SPLASH_TIME_OUT);
            finish();

        }catch (Exception e){
            System.exit(1);
        }
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
        }
    };

    Thread thread = new Thread(myRunnable);
    thread.start();




    }catch (Exception e){
        System.exit(1);
    }

}
0

Better to use CountDownTimer. Inside onFinish() method launch your activity.Check below:-

 private void Start_Timer(String duration) {
        Log.d(TAG,"duration value"+Long.parseLong(duration));
        new CountDownTimer(Long.parseLong(duration), 10000){
            public void onTick(long millisUntilFinished){
                Log.d(TAG,"counting");
            }
            public  void onFinish(){
                Log.d(TAG,"inside finish");
                Intent intent =new Intent(getApplicationContext(),Result_Summary.class);
                startActivity(intent);
            }
        }.start();
    }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Parveen Kumar
  • 175
  • 3
  • 5