Reputation: 501
Is it possible to pass an Android Account object from one activity to another via an Intent? If so, how? Is it good programming practice?
Upvotes: 0
Views: 1420
Reputation: 1210
You can send in the account to other activities through Intent
First get the account if the user is already signed in
GoogleSignInAccount account;
account = GoogleSignIn.getLastSignedInAccount(this);
Then pass the account as an Extra in the Intent
Intent intent = new Intent(this,ReceiverActivity.class);
intent.putExtra("ACCOUNT",account);
startActivity(intent);
And in the ReceiverActivity
GoogleSignInAccount account =getIntent().getParcelableExtra("ACCOUNT");
Then you can get other details from the user's profile like
String personName = account.getDisplayName();
String personGivenName = account.getGivenName();
String personFamilyName = account.getFamilyName();
String personEmail = account.getEmail();
String personId = account.getId();
Uri personPhoto = account.getPhotoUrl();
Upvotes: 2
Reputation: 48871
If you mean android.accounts.Account
then 'Yes', as it implements Parcelable
, you can pass it as an extra of an Intent
using the putExtra(String name, Parcelable value)
method of Intent
.
Whether it's good programming practice or not...that depends entirely on why you want to do it and what the Activity
that receives it will do with it.
Upvotes: 3
Reputation: 917
Could you provide a code sample, and explain a little what exactly you are using this passing for ?
You might want to optimize the flow of your application, in order to process the account info directly, and create the account object when you need it.
Upvotes: 0