5

I'm working on project to my university, and I would like to know how save some information about facebook user like name, e-mail and picture. Then how I can use this information in another Activity? As you can see my code i can show info in the same activity that i logged but i cant have in another activity

MainActivity.java

public class MainActivity extends ActionBarActivity {

public static final int INDEX_SIMPLE_LOGIN = 0;
public static final int INDEX_CUSTOM_LOGIN = 1;

private static final String STATE_SELECTED_FRAGMENT_INDEX = "selected_fragment_index";
public static final String FRAGMENT_TAG = "fragment_tag";
private FragmentManager mFragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mFragmentManager = getSupportFragmentManager();

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if (id == R.id.action_simple_login) {
        toggleFragment(INDEX_SIMPLE_LOGIN);
        return true;
    }
    if (id == R.id.action_custom_login) {
        toggleFragment(INDEX_CUSTOM_LOGIN);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void toggleFragment(int index) {
    Fragment fragment = mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    switch (index) {
        case INDEX_SIMPLE_LOGIN:
            transaction.replace(android.R.id.content, new FragmentSimpleLoginButton(), FRAGMENT_TAG);
            break;
        case INDEX_CUSTOM_LOGIN:
            transaction.replace(android.R.id.content, new FragmentCustomLoginButton(), FRAGMENT_TAG);
            break;
    }
    transaction.commit();
}

}

MyApplication.java

public class MyApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
}

/**
 * Call this method inside onCreate once to get your hash key
 */
public void printKeyHash() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo("vivz.slidenerd.facebookv40helloworld", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("VIVZ", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

}

FragmentSimpleLoginButton.java

public class FragmentSimpleLoginButton extends Fragment {
private TextView mTextDetails;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Log.d("VIVZ", "onSuccess");
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        mTextDetails.setText(constructWelcomeMessage(profile));
    }

    @Override
    public void onCancel() {
        Log.d("VIVZ", "onCancel");
    }

    @Override
    public void onError(FacebookException e) {
        Log.d("VIVZ", "onError " + e);
    }
};


public FragmentSimpleLoginButton() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mCallbackManager = CallbackManager.Factory.create();
    setupTokenTracker();
    setupProfileTracker();

    mTokenTracker.startTracking();
    mProfileTracker.startTracking();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_simple_login_button, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    setupTextDetails(view);
    setupLoginButton(view);
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    mTextDetails.setText(constructWelcomeMessage(profile));
}

@Override
public void onStop() {
    super.onStop();
    mTokenTracker.stopTracking();
    mProfileTracker.stopTracking();
}

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

private void setupTextDetails(View view) {
    mTextDetails = (TextView) view.findViewById(R.id.text_details);
}

private void setupTokenTracker() {
    mTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
            Log.d("VIVZ", "" + currentAccessToken);
        }
    };
}

private void setupProfileTracker() {
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
            Log.d("VIVZ", "" + currentProfile);
            mTextDetails.setText(constructWelcomeMessage(currentProfile));
        }
    };
}

private void setupLoginButton(View view) {
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button);
    mButtonLogin.setFragment(this);
    mButtonLogin.setReadPermissions("user_friends");
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);
}

private String constructWelcomeMessage(Profile profile) {
    StringBuffer stringBuffer = new StringBuffer();
    if (profile != null) {
        stringBuffer.append("Welcome " + profile.getName());
    }
    return stringBuffer.toString();
}
Miguel Lemos
  • 80
  • 2
  • 9
  • 1
    store the imagelink ,email and picture in shared preference and retrieve them in another activity – kgandroid May 04 '15 at 06:58
  • If you want to save it permanently then use `SharedPreference` or if you are getting this email, imagelink values every session then better just pass those data to other activity using `Intent` – Kunu May 04 '15 at 07:17
  • Hi mate did you get email address from FB info and how ? plz can you share its sample code if you got email – Abdulkhodiy Nematov May 16 '15 at 06:08

2 Answers2

4

if you want to send the info to the next activity you can add it to the intent with a bundle.

ACTIVITY:

Intent i=new Intent(Activity.this, SecontActivity.class);
i.putExtra("email", email);
startActivity(i);

SecontActivity:

Intent intent = getIntent();
String email = intent.getStringExtra("email");

if you want the info in all your activities then save it in SharedPreferences

Activity:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("email", email);
editor.commit();

SecondActivity:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String email = sharedPref.getString(email, defaultValue);
ilan
  • 4,402
  • 6
  • 40
  • 76
1

Store the info such as name, imageurl and other profile details in a database or (sharedpreferences as indicated here) for access across different activities. This information can be fetched at any stage once saved throughout the history of the app unless the data from the app is cleared.

I would go for the database as it would more structure and can potentially provide multi-user support, incase you want to implement profile switching in your app.

Community
  • 1
  • 1
Vaibhav Singhal
  • 888
  • 9
  • 13