Facebook on Android without using fragments

I would like to implement complete control of basic things about facebook in the application, therefore requesting personal information, friends list and posting on the wall. All of this should be done without using snippets using API 3.0.

Right now, I'm stuck with the only example I have that does this, which is SessionLoginSampleActivity. My program right now is nothing more than this example adapted to my layout, you can find it at the end of the post. Unfortunately this example does not go beyond authentication and I cannot figure out how to make an asynchronous conversation with Facebook and get user data and all that.

Is there a complete example somewhere?

Once authenticated, how can I asynchronously retrieve the user's picture, my name, and my friends list without fragments? Where should I write? Create a new listener?

The question may sound strange and basic, but I am really going crazy with the facebook API, which looks so powerful and yet very obscure to my eyes.

Please don't answer me "use snippets!" :)

Many thanks!

public class FbActivity extends Activity {
private Button buttonLoginLogout, b_friends, b_challenge;
private TextView t_instructions, t_username;
private ProfilePictureView profilePictureView;
private Session.StatusCallback statusCallback = new SessionStatusCallback();

public FbActivity() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.facebook_activity);
    findAll();

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

    updateView();
}
@Override
public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}

private void findAll() {
    buttonLoginLogout = (Button)findViewById(R.id.buttonLoginLogout);
    t_instructions = (TextView)findViewById(R.id.instructionsOrLink);
    b_friends = (Button)findViewById(R.id.buttonFindFriend);
    b_challenge = (Button)findViewById(R.id.buttonFindNewChallenge);
    profilePictureView = (ProfilePictureView)findViewById(R.id.profilePicture);
    t_username = (TextView)findViewById(R.id.textViewUserName);
}



private void setVisibility(boolean opened) {
    if (opened) {
        buttonLoginLogout.setText(R.string.logout);
        buttonLoginLogout.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickLogout(); }
        });
        t_instructions.setVisibility(TextView.INVISIBLE);
        b_friends.setVisibility(Button.VISIBLE);
        b_challenge.setVisibility(Button.VISIBLE);
        t_username.setVisibility(TextView.VISIBLE);
        profilePictureView.setVisibility(ProfilePictureView.VISIBLE);
    } else {
        buttonLoginLogout.setText(R.string.login);
        buttonLoginLogout.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickLogin(); }
        });
        t_instructions.setVisibility(TextView.VISIBLE);
        b_friends.setVisibility(Button.INVISIBLE);
        b_challenge.setVisibility(Button.INVISIBLE);
        t_username.setVisibility(TextView.VISIBLE);
        profilePictureView.setVisibility(ProfilePictureView.INVISIBLE);
    }

}
private void updateView() {
    Session session = Session.getActiveSession();
    setVisibility(session.isOpened());
}

private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(this, true, statusCallback);
    }
}
private void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
}   
private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
    }
}

}

      

+3


source to share


4 answers


We need to maintain more BW compatibility than this. What we did was to pull the facebook API v1.2.2 version from github and use that instead. The docs are harder to find, but the sample apps are directly used ... and no snippets are visible anywhere.

git clone git://github.com/facebook/facebook-android-sdk.git
cd facebook-and*
git checkout v1.2.2

      



It looks like we'll have to add a few other things as well, like a newer version of images to their library project, but that doesn't really matter.

+2


source


Let's say you first have a page called logged_off.xml where you only have your loginButton and your goal is to log in, and then after you've registered correctly, change your view to another page called logged_in.xml ...

so you have:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logged_off);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

loginButton = (Button) findviedbyid(R.id.loginButton);
loginButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) { 
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(PERMISSIONS));  
}
    });

//here we check if our session exists. if it does exists we directly go to our page logged_in.xml otherwise we stay in logged_off.xml waiting for the user to click on our loginButton

Session session = Session.getActiveSession();
if (session == null) {
    if (savedInstanceState != null) {
        session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
    }
    if (session == null) {
        session = new Session(this);
    }
    Session.setActiveSession(session);
}else{
if(session.session.isOpened()){
setContentView(R.layout.logged_in);
}
}
}

      

This function is called after we try to log in, so here we check if we are really logged in, and if we did, we just changed our page to logged_in.xml. So change this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

      

:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

if(session.session.isOpened()){
    setContentView(R.layout.logged_in);
}
}

      

of course you can change setContentView (R.layout.logged_in); with whatever you want.

hope this helped!

PS: keep all these functions:

@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}

private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
    //updateView();
}
}

      

+2


source


check this link for Facebook tutorial to post to Facebook wall, you can get basic idea for Facebook integration using this link:

http://www.londatiga.net/it/how-to-use-facebook-sdk-to-post-status-from-android/

after that you can check this link for more details on user details:

Extract username and gender from Facebook in android

0


source


I suggest using the Facebook developer tutorials as your reference and just strip out all the snippet-related stuff like fragments manager

save methods UiLifecycleHelper

and onSessionStateChange

on the other hand the necessary stuff and let you know the session state and its changes.

Here is an example onSessionStateChange and how you can change it to start a new activity, I am creating an app without fragments

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

                if (state.isOpened()) { 
                    // If the session state is open:
                    // Show the SendRequest Activity
                    Intent sendRequest = new Intent(this, SendRequest.class);  // If session isOpened we launch the INTENT to Pick Friends
                    startActivity(sendRequest);
                }else if (state.isClosed()){
                    uiHelper.onResume();
                }
        } 

      

0


source







All Articles