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 } } 
4
  • So are you having problems adding the delay, or doing the redirect, or both? Commented Mar 11, 2016 at 8:53
  • Similar question asked here: start second activity Commented Mar 11, 2016 at 8:55
  • Possible duplicate of How to start new activity on button click Commented Mar 11, 2016 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 Commented Mar 13, 2016 at 20:59

5 Answers 5

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

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
Is the Flogin an Activity?
The intent needs the current context as argument, if you are in a fragment use getActivity()
You should use getActivity() for current context if you're inside a fragment.
Pleasure's all mine :)
1

Simply call a new activity through intent:

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

2 Comments

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
If calling through a fragment then you should use this.getActivity() instead. So, Intent i = new Intent(this.getActivity(), HomeActivity.class); startActivity(i); finish();
1

Check this:-

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

Comments

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 } 

Comments

0

In Kotlin;

 val r = Runnable { startActivity(Intent(this, AuthorActivity::class.java)) } val h = Handler() h.postDelayed(r, 10) 

Comments