Skip to content
Advertisement

Android: Redirect to another Activity after delay

So I am developing a simple app for a college project, And I have been able to integrate a Facebook login using fragments.

But I now am stuck trying to redirect the user after they login. I simply want to redirect them to the second activity page

Here is my code for the Facebook login success

private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
            display.setText("Welcome: " + profile.getFirstName());
            //Redirect to Second Activity

        }

} 

Advertisement

Answer

To make a delayed transition use Handler class’s postDelayed(Runnable r, long delayMillis) method, for example:

Java

Runnable r = new Runnable() {
    
    @Override
    public void run() {
        // if you are redirecting from a fragment 
        // then use getActivity() as the context.
        startActivity(new Intent(CurrentActivity.this, TargetActivity.class));
    }
};
    
Handler h = new Handler();
// The Runnable will be executed after the given delay time
h.postDelayed(r, 1500); // will be delayed for 1.5 seconds

Kotlin with Anko

val someThread = Runnable {
    startActivity(intentFor<TargetActivity>())
}

Handler().postDelayed(someThread, 1500)
User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement