Android Activity choose - android

I need to choose if I should run one activity or another depends on authorization. If you have no account in AccountManager I want to show RegistrationAcitivity in other situation I want to show LoginActivity. What is the best pattern to do that? Where I need to put the code which is checking that? In one of those activities?

You can check the condition is user is already register then move to AccountManager .
Intent intent = new Intent(this,AccountManager.class);
startActivity(intent);
if not then redirect him to RegistrationAcitivity
Intent intent = new Intent(this,RegistrationActivity.class);
startActivity(intent);
For that you can set flags.
or store data either in SharedPreferences or Database

You can put the code in Registration Activity, check for accounts there and if available redirect him to login activtiy

Related

Android back to activity after validate

I'm making a registration form with 5 steps (5 activities, Similar to facebook signup form).
The 1st activity is to put email
The 2nd activity is to put name and so on. After the user complete all steps, i validate the field email (to avoid being duplicated) from the server side. What i want is, if the user email exist, i want to go back to 1st activity so the user can enter other email and the submit the form again to the server. Whats the best way to do that?
Thanks
In your LastActivity after some action.
Intent intent = new Intent(this,FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent );
This flag clear recent Activity in the activities stack.

Android: userId - GET PHP alternative?

so I have an app where users have accounts/profiles, when say you type name and surname in search it would bring to activity called ProfileViewer and display that users details.
In PHP I would have link like:
John Sample
So when user clicks on that - it would go to PHP page called profile.php and load data from MySQL database WHERE userid=xxx.
So what's the alternative in Android? I am a bit lost, what are some of my options on how to identify which userID is Activity loading?
Could I use putExtra("userId", xxx) and then use getExtra()?
In other words: How can I make one activity act like page which accepts 1 param and then uses it on opening?
Intent extras are generally the way to go in these cases.
From the first activity you would:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("userId", 1);
startActivity(intent);
And you then retrive the information in the second activity like this:
getIntent().getIntExtra("userId");

Correct way to track users and logout app

I am trying to code a native app with social logins as you can see the below image.
Now I'm able to authenticate users from my FirstActivity and sending the authenticated users to next SecondActivity.
Here comes my issue.
I have a single logout button on my SecondActivity
Now how do I know which account user clicked and how do I make them logout ?
Any suggestions are most welcome.
In your first activity make a string variable and set it to either facebook or either google + depending upon which the user picks to login. Pass in this variable as an extra with the intent to start the second activity.
So in your first activity you need something like this:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("id","FirstKeyValue");
myIntent.putExtra("name","SecondKeyValue");
startActivity(myIntent);
In your second activity you need something like this to retrieve the value :
Intent intent = getIntent();
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

How to display a welcome message including username from another activity in android?

Here is what I want to do, I have an android login form with username and password, after the user enters his credentials and login, the next form should display on top of the page welcome,+username entered from the login page! can someone please help me??
I'm new to android development and don't know how to go about this. Thanks
You can use intent to pass username from one activity to another.
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("username","enteredusername");
startActivity(intent);
And in SecondActivity
String name = getIntent().getStringExtra("username");
Now use the name and display the welcome +name
http://developer.android.com/training/basics/firstapp/starting-activity.html
In order to send data from one activity to another, you need to use intents. So, in your login activity, you would launch your welcome activity like this:
Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.putextra("userName","value");
startActivity(intent);
Then, in your welcome activity, you need to retrieve the extra data, like so:
String data= getIntent().getStringExtra("userName");
There are a multitude of examples that can be found via Google, and many questions similar/identical to this have been answered previously on SO.
You can store the username in sqlite or sharedpreferences to get the username across all your application.

Question regarding account creation and sync on Android

I've been reading the sample code from the dev docs on Android's site, specifically this:
http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/authenticator/AuthenticatorActivity.html
Which is the sole activity of the sample app. It refers to an intent in the onCreate method. I don't understand where this intent is coming from, or what it should contain if this is the only activity the app utilizes.
Log.i(TAG, "loading data from Intent");
final Intent intent = getIntent();
mUsername = intent.getStringExtra(PARAM_USERNAME);
mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
mRequestNewAccount = mUsername == null;
mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
That's the block of code working with the intent. Why would you have an intent for the only activity in the app? Is this app called in an unusual way? The Manifest does not include an intent filter for the activity... I guess I'm just a bit lost on this whole thing! If someone could set me straight that'd be great, thanks.
Why would you have an intent for the only activity in the app?
getIntent() gets you the intent that started this activity.
Is this app called in an unusual way?
I guess this activity is called programmatically from another app or activity, since it has been passed some extra data: getStringExtra() is used to extract some data from the intent that started it. putExtra.. and getExtra.. is a way to pass data between activities when they are started.
In that specific example, the intent is sent from the addAccount method in Authenticator.java. That method is called by the OS when you click the Add Account button in the Accounts & sync settings screen and choose your account type.

Categories

Resources