Android Man
Android Man

Reputation: 319

How to redirect to next activity

I am making an Android App where the user should login through FACEBOOK. The problem is that when the user logs in through FACEBOOK he comes back to the same Activity from where he has logged in instead of going to the next Activity. Here is my code:-

MainActivity

package com.example.abc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class MainActivity extends Activity {

    // Your Facebook APP ID
    private static String APP_ID = "xxxxxxxxxxxxxxxx"; // Replace with your App ID

    private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);

    /** Getting a reference to the ViewPager defined the layout file */        
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);
    Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                //Intent in = new Intent(MainActivity.this, LoginFaceBook.class);
                //startActivity(in);
                loginToFacebook();

        }
    });

    Button btn2 = (Button) findViewById(R.id.button2);
    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            loginToTwitter();
        }

        private void loginToTwitter() {
            // TODO Auto-generated method stub

        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);




        Intent inst = new Intent(getApplicationContext(), AnotherActivity.class);
        startActivity(inst);


        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();

                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
   }
}

Upvotes: 0

Views: 371

Answers (1)

afontaine
afontaine

Reputation: 1059

Looks like you get null for an access token, which then sends you off to authorize with FB, skipping where you would launch your next activity. In the onComplete method of your DialogListener, you commit your access token and to your SharedPreferences and exit the loginToFacebook method.

One strategy to go to the next activity might be to try and launch it outside of your loginToFacebook method, directly after you call it (checking to make sure you did get an access token back, of course).

Upvotes: 1

Related Questions