Setting order of Activities and going back from them - android

I have a few activities:
Splash screen
Login
Registration
Dashboard
If the user has never logged in, it will go like this:
Splash screen > Login > Registration > Dashboard
When I back from the Dashboard, it should exit the app, skipping through the other activities.
noHistory on the Login page doesn't work here because sometimes the user will back from Registration.
If the user has previously logged on, it should go like this:
Splash screen > Dashboard
If the user logs out, perhaps to use another account, then it should go:
Dashboard > Login > Dashboard
But if the user goes back from the new Dashboard, it shouldn't enter the previous account's Dashboard.
Aside from that, my app contains multiple modules, some of which don't have access to other modules, so a solution that can work between modules would be helpful.
I have tried a mix of finish() and startActivityForResult() and trying to check where the activities return from but it felt very hacky, time-consuming, and it messes up on new use cases. Are there better ways?

You should do something like this
In your splash activity I have a method like :
#Override
public void loadLoginScreen() {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
Then in Login activity (i am using firebase so i check if the user is null or not you may apply your logic to check the same )
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get Firebase auth instance
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, HomeScreenActivity.class));
finish();
}
// set the view now
setContentView(R.layout.activity_login);
}
and when I do the logout I do somewhat like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.milogout:
FirebaseAuth.getInstance().signOut();
startActivity(LoginActivity.class);
finish();
break;
}
return true;
}

Previous activities need to be finished on moving to Dashboard. Call before starting new activity of Dashboard
finishAffinity();
This finish current activity as well as all activities immediately below it.

Related

Intent is not working, when the state of a button is checked

I am trying to build a flight ticket booking app.
Here I have two options either user selects one-way or round-way(both sides). When a user selects one-way I need to go to the next screen where all one-way flight details will be shown. Other-wise if a user selects round-way then I will show the details of the round trip to the user.
But if I select one-way(Button) and check it's Pressed state it shows False, So I am unable to navigate the user to the next screen and the same flaw is happening for the round-way.
MainActivity
private void searchClickHandeler(){
//Set Onclick Listener
b_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
*when One way is pressed it is suppose to go to the One way Suggestion Activity*
if (b_oneway.isPressed()){
Toast.makeText(FlightActivity.this,"Oneway Button is pressed",Toast.LENGTH_LONG).show();
Intent oneway = new Intent(FlightActivity.this,FlightSuggestionActivity.class);
startActivity(oneway);
} else if (b_returnway.isPressed()){
Toast.makeText(FlightActivity.this,"RoundTrip Button is pressed",Toast.LENGTH_LONG).show();
Intent returnway = new Intent(FlightActivity.this,FlightRoundTripSuggestionActivity.class);
startActivity(returnway);
}
}
});
}
try this
Intent intent = new Intent(v.getContext(), FlightSuggestionActivity.class);
startActivity(intent);

how do i change the landing page of the app after the user has signed in

i have created android app that require users to enter their phone numbers when they first use the app, now i am storing that info using SQL lite.the problem is every time they open the app it requires their phone number, and I want the app to just automatically login without asking for the phone number again, kind of like whats app.
Its not about landing page change. The activity which has a category "LAUNCHER" in manifest file always opens first. In that Activity .java file , You can make a check , whether the value for user is available in sqlite or not. if available perform intent to next page .. Check this link too ......Android check user logged in before, else start login activity
Either set a splash activity or another blank activity as your initial activity.
Then store a boolean in your app's shared preference to identify whether the application
is loading first time or not. Based on that boolean value, move to phone number entry screen or
your desired screen.
In your "login" activity you should look for the number in the db, if it is there, you open a new activity, if not, you ask the user. You can use setVisibility(int) in your "ask" views to not show them while looking in the db, and then, if you don't find the number, you show them.
You have to use sharedPrefrences where you can store whether user has stored his number.
When user open the app for the first time and enter his number, then store value in sharedprefrences.
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("prefrence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("value", "selected"); editor.commit();
When user open the app, Splash screen will check the value in sharedprefrences. If user already had put his number then he will be re-directed to home screen rather than phone number screen.
Splash Screen:
Context mContext;
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mContext = SplashScreen.this;
new Handler().postDelayed(new Runnable() {
/*Showing splash screen with a timer. This will be useful when you
want to show case your app logo / company*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String value = (shared.getString("value", ""));
if(value!=null && !value.equals("")){
/*Re-Direct to Home Screen after Login*/
Intent intent = new Intent(mContext,MainActivity.class);
startActivity(intent);
}
else{
Intent intent = new Intent(mContext, LoginActivity.class);
startActivity(intent);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}

App freezes after extending Google Plus login Activity

Am using the Google+ login feature in my app. The Login functionality is in my LoginActivity, and the logout functionality is called from another activity, an Activity containing Fragments. To be able to call the logout method, the Activity containing the Fragments extends the Login Activity(else the app crashes). These methods are in the Login Activity:
// Initializing google plus api client (from onCreate method of Login Activity)
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
//Called when a connection is successful
public void onConnected(Bundle arg0) {
mSignInClicked = false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
//Get user's information
getProfileInformation();
Intent intent = new Intent(getApplicationContext(), UtilityActivity.class);
startActivity(intent);
}
When I run the app, the Toast is never dismissed.When I comment out the toast and run the app again, none of the widgets respond. Am thinking the app stays in an onConnected state?.
I don't know why this happening, but am suspecting it's because am calling the onCreate method or the onConnected method twice?
The call to signout is being made from the onOptionsItemsSelected method of the Activity containing the Fragments like this:
#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();
switch (id) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Settings selected", Toast.LENGTH_LONG).show();
return true;
case R.id.action_logout:
signOutFromGooglePlus();
return true;
case R.id.action_revoke_access:
revokeGplusAccess();
return true;
}
Method called when the signin button is clicked:
private void signInWithGooglePlus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
You mention that the issue is occurring in UtilityActivity, which is launched by LoginActivity in onConnect().
Your likely issue (without seeing the code in UtilityActivity) is that UtilityActivity does not have a reference to the connected GooogleApiClient from LoginActivity. So, while you have a valid connection in LoginActivity, you do not in UtilityActivity. Remember, extending LoginActivity doesn't mean you share its fields. It's more like sharing code w/o having to copy/paste.
There are several options on how to proceed - pass a copy of mGoogleApiClient in the Intent that launches UtilityActivity, make the activity that controls login/logout a service that all your other activities can use, pass only the information that UtilityActivity needs in an Intent when launched, have each Activity manage their GoogleApiClient connections directly...

startActivity to relaunch HomeActivity from another activity in the same application doesn't work in Gingerbread

In my application I have used some code from the iosched 2012 app. In specific the starting workflow is the following:
1.The user presses the launcher icon of the app
2.HomeActivity checks if the user is authenticated. If he/she is not, it starts the Authentication activity, passing it intent to it and finishes itself
3.When the login process is successful, the authenction activity starts an activity in order to start the HomeActivity and finishes itself
4.HomeActivity checks again if the user is authenticated and displays the home screen of the application.
The following code works like a charm in API Level > 11. Today, I tried the app in a Gingerbread and it fails. Step 3 works, but although the HomeActivity starts it's not brought to front. You have to use the recent list and choose the application in order to see the homeactivity and its now displayed content.
Here's the code and check from the HomeActivity in the oncCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!AccountUtils.isSystemAuthenticated(this)) {
AccountUtils.startSystemAuthentication(this, getIntent());
finish();
} else if(!AccountUtils.isAppAuthenticated(this)) {
AccountUtils.startAppAuthentication(this, getIntent());
finish();
}
if(isFinishing()) {
return;
}
setContentView(R.layout.activity_main);
...
}
}
The method invoked in the Authentication activity after the login process is completed
protected void handleLoginSuccess(LoginServiceResponse response, String username, String password) {
if(....) {
if(mFinishIntent != null) {
mFinishIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mFinishIntent.setAction(Intent.ACTION_MAIN);
mFinishIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mFinishIntent);
}
finish();
} else {
super.handleLoginSuccess(response, username, password);
}
}
Where the mFinishIntent member variable is the intent passed from the HomeActivity (using getIntent())
As I mentioned, in API Level > 11, this works well, and the breakpoint in HomeActivity's onCreted method is hit twice, while in a Gingerbread phone, is hit only once (only when the application starts).
Do I have to use another flag or do you have any other idea of what's going on?
Thanks
What is probably happening is that the activity is only created when the app is started and then when you go back to it from the Authentication activity, it is simply resumed. Try putting the authentication checking code in HomeActivity in the onResume() method.
Here is some more info: http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

Android shared preferences conditional activity switching

I have an Android app which I use to register users on my web site. My first task is to register a user if my shared preferences file shows there is no registered user information.
If my app has a registered user, I provide the following code to simply and automatically switch to a "homepage" activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
if( USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, "") == null && USERPREFERENCES.getString(USERPREFERENCES_USERID, "") == null && USERPREFERENCES.getString(USERPREFERENCES_USERNAME, "") == null){
//setContentView(R.layout.signin);
Toast.makeText(SignIn.this, "testing...", Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(SignIn.this, Confirmed.class);
startActivity(intent);
}
... other code
So, from my default activity, signin.java, the app will either switch to the Confirmed activity or stay on and display the signin activity.
My problem is, when the system works and I get switched to the the Confirmed activity, I provide a logout onclick listener which is below:
signout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//USERPREFERENCES.cl
Toast.makeText(Confirmed.this, "signout responding!", Toast.LENGTH_LONG).show();
USERPREFERENCES.edit().clear().commit();
}
});
It responds and clears all my shared preferences variables. But, when I use my menu to manually switch to the sign-in activity, I still get switched back to the Confirmed activity.
This happens even though I can confirm the variables are empty.
This hardly ever will be true:
USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, "") == null
What if you use this instead?
if( USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, null) == null && USERPREFERENCES.getString(USERPREFERENCES_USERID, null) == null && USERPREFERENCES.getString(USERPREFERENCES_USERNAME, null) == null){
//setContentView(R.layout.signin); TRY TO AVOID DOING THIS THING!!!!!
Toast.makeText(SignIn.this, "testing...", Toast.LENGTH_LONG).show();
}else...
Also, as a recommendation... instead of being switching between activities... what if you create just a Signing.java activity and put a ViewFlipper in its layout. That way your app will be not only faster but also easier to maintain.
This is Because When you will switch back to LoginActivity, this will be resumed instead of being created , Means your Login code which you written inOnCreate will not be called because Dthis time Overrider OnResume has been called , not onCreate .
So either write this code again in onResume or call finish() before moving to second activity , so that next time it will call onCreate()
If you navigate back to the first activity, the onCreate is not called again (unless the activity was destroyed for lack of resources). Move the authentication code in onResume.

Categories

Resources