Reputation: 881
I followed a Facebook tutorial to make a loginbutton for my app, and it works with login, but I have some questions that I would need help from you guys.
I have one Activity & one Fragment. When you start up my app, you will see a png-image that is the logo, plus a 'login to facebook' button. When you press the login button, you will be sent to Facebook for the auth, and after the log in you will be send back to the MainActivity again.
Now the MainActivity have the logo and the Facebook button says 'log out'. So this is fine, but my question is:
What is the best thing to do AFTER you logged in??
Create a new class that you will be sent after the login, or be send back to the MainActivity? What do real programmer do after the auth?
And if its better to send back to the MainActivity, how then can I remove the logo png image after login? I mean, that is nice to have before you login, but after the login, the picture should be gone. Same thing with the Facebook LOGOUT button, how do I remove it, or atleast put it somewhere else? I though to make an Actionbar to but the logout button there.
This is the core I use for the Activity & the Fragment. So, should I do a new class to send the user after login, or should i send the user mack to the activity, and if yes, how remove the logo/logout button after logged in? Thank you.
EDIT:
IF the best way is to create a new class and send the user to the new class after login, WHERE in the code should I write that? I tried OnClickListener, but it never sends the user to the new class AFTER LOGIN. Thank you.
LoginACTIVITY.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.facebook.widget.LoginButton;
public class LoginActivity extends FragmentActivity {
private LoginFragment loginFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
loginFragment = new LoginFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, loginFragment)
.commit();
} else {
// Or set the fragment from restored state info
loginFragment = (LoginFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
}
LoginFRAGMENT.
public class LoginFragment extends Fragment {
private static final String TAG = "LoginFragment";
private UiLifecycleHelper uiHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_login, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("public_profile" , "user_birthday" , "user_friends"));
return view;
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
EDIT 2:
I think I got it to work. This is what I did, but dont know if its right or not.
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
finish();
And for the finish(); I did.
private void finish() {
}
Upvotes: 1
Views: 1068
Reputation: 135
You should create a new class(Intent) and send the user to the new class(Intent) after login, in the onSessionStateChange() method also if you want to send data obtained from the facebook to next intent use the bundle approach. check this example
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
// Request user data and show the results
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Intent pit = new Intent(getApplicationContext(), HomeActivity.class);
pit.putExtra("Fb_id", user.getId());
pit.putExtra("user_name", user.getName());
startActivity(pit);
}
}
}).executeAsync();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
Upvotes: 1
Reputation: 2530
Do it as it mentioned Tutorial
I hope this will solve your problem.
Upvotes: 0
Reputation: 149
I think the best thing to do in usability is to take the user where he should be and i'm pretty sure that it's not the login screen again. You logged him for a reason, so try to accomplish that.
As to your code, @varsha-venkatesh code should accomplish that.
Upvotes: 0
Reputation: 139
Try the following:
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Intent intent = new INTENT(context, NewActivity.class);
context.startActivity(intent);
finish(); // back button shouldn't bring you back to this fragment
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
Upvotes: 2
Reputation: 280
ok this is what i will do. First activity that my app will have is LoginActivity.java. This activity will contain the logo and facebook login buttons. When user clicks and signs in using facebook, i call the MainActivity.java[well this is the main homescreen of my app]. When user clicks back button here i dont go back to LoginActivity.java and i'd rather show an app quit alert box.
Upvotes: 0