Android: redirect to another activity after delay

So, I am developing a simple application for a college project, and I was able to integrate Facebook login with fragments.

But now I'm stuck trying to redirect the user after logging in. I just want to redirect them to the second page of activity

Here is my code for 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 } } 
+5
source share
4 answers

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

  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 
+4
source

Just call a new action with the intent:

 Intent i = new Intent(LoginActivity.this, HomeActivity.class); startActivity(i); finish(); 
+2
source

Check this: -

 new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i=new Intent(CurrentActivity.this,Next.class); startActivity(i); } }, 3000); 
+2
source

The handler allows you to send and process the Message and Runnable objects associated with the MessageQueue stream. Each handler instance is associated with one thread and this thread's message queue. when you create a new handler, it is associated with the thread / message chain the thread that creates it - from now on, it will deliver messages and runnables to this message queue and execute them as they exit the message queue.

You can easily use the postDelayed Handler .

 Handler hd = new Handler(); hd.postDelayed(new Runnable() { @Override public void run() { // Add Your Intent } }, 2000); // Time Delay ,2 Seconds } 
+1
source

Source: https://habr.com/ru/post/1244867/


All Articles