7

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

        }

} 
  • So are you having problems adding the delay, or doing the redirect, or both? – 1615903 Mar 11 '16 at 08:53
  • Similar question asked here: [start second activity](http://stackoverflow.com/questions/13194081/how-to-open-a-second-activity-on-click-of-button-in-android-app) – jam Mar 11 '16 at 08:55
  • Possible duplicate of [How to start new activity on button click](http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – David Medenjak Mar 11 '16 at 17:21
  • @DavidMedenjak How is it a duplicate the question u posted is about attaching to a button click ? mine asks about redirecting after a time –  Mar 13 '16 at 20:59

5 Answers5

7

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)
Genusatplay
  • 761
  • 1
  • 4
  • 15
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • Yeah see the problem is the same as what I had before my intent doesnt work " Intent i3 = new Intent(FLogin.this, SecondActivity.class); startActivity(i3);" Flogin is not an enclosed class/ and if I change it to the fragement name it just breaks the whole line saying cannot resolve –  Mar 11 '16 at 08:59
  • Is the Flogin an Activity? – Ahmed Hegazy Mar 11 '16 at 09:08
  • 1
    The intent needs the current context as argument, if you are in a fragment use `getActivity()` – 1615903 Mar 11 '16 at 09:09
  • You should use `getActivity()` for current context if you're inside a fragment. – Sharp Edge Mar 11 '16 at 09:10
  • Pleasure's all mine :) – Sharp Edge Mar 11 '16 at 09:14
1

Simply call a new activity through intent:

Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
finish();
Mansha Chuttani
  • 381
  • 3
  • 7
  • Yeah see the problem is the same as what I had before my intent doesnt work " Intent i3 = new Intent(FLogin.this, SecondActivity.class); startActivity(i3);" Flogin is not an enclosed class/ and if I change it to the fragement name it just breaks the whole line saying cannot resolve –  Mar 11 '16 at 09:01
  • If calling through a fragment then you should use this.getActivity() instead. So, Intent i = new Intent(this.getActivity(), HomeActivity.class); startActivity(i); finish(); – Mansha Chuttani Mar 22 '16 at 08:31
1

Check this:-

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

                      Intent i=new Intent(CurrentActivity.this,Next.class);
                      startActivity(i);
                  }
              }, 3000);
Chandan
  • 187
  • 1
  • 8
1

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

You can use Handler postDelayed Method easily .

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

               // Add Your Intent

             }

            }, 2000); // Time Delay ,2 Seconds 
     }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

In Kotlin;

  val r = Runnable {

        startActivity(Intent(this, AuthorActivity::class.java))
    }
    val h = Handler()

    h.postDelayed(r, 10) 
Murat Çakır
  • 150
  • 1
  • 14