private void startUpTasks() {
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", true);
// If the activity has never started before...
if (isFirstRun) {
// Launch app intro
Intent i = new Intent(LoginActivity.this, EnterInfoActivity.class);
startActivity(i);
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).apply();
initializeUserInfo();
} else {
getUserInfo();
Intent myIntent = new Intent(LoginActivity.this, SplashScreen.class);
LoginActivity.this.startActivity(myIntent);
}
}
Basically I have it narrowed down that the check goes straight to the else statement which doesn't allow a user to register for the first time. I'm using Firebase Auth to register a new user and store them in Firebase's Database.
Any ideas on how to fix this?
Okay look I will try to help you understand what you should do:
I assume that you want the user to go to EnterInfoActivity only if they didn't register before, and you are using sharedPreferences to do so. If that is the case:
This code:
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).apply();
Must be added after the user registered the details in the EnterInfoActivity
Other than that
If you want to test the functionality again, you must uninstall the app to clear shared preferences data and run the app again to test again as if you are a new user that installed the app, this way if-statement must run instead of else.
Related
i want to make a swiggy type app. i have integrated gmail login in my login screen and it changes to dashboard activity on success but when i click on account tab it goes back to dashboard screen instead of account activity. pls help......
login screen
dashboard
if you want any portion of code just comment and i will update
//login screen part
//check if already signed in using google
account = GoogleSignIn.getLastSignedInAccount(this);
if(account!=null) {
finish();
Intent intent = new Intent(this, DashboardActivity.class);
startActivity(intent);
return;
}
//onclicklistener added
//method
private void googleSignin() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivity result(//params provided){
if(googleLogin){
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
googleLogin = false; //set to false so that it can be set true again if login is actually successful
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
sessionManager.setLogin(true);
googleLogin = true;
Intent intent = new Intent(this,DashboardActivity.class);
intent.putExtra("googleLogin", googleLogin);
startActivity(intent);
finish();
}
//Dashboard part
Intent i = new Intent(DashboardActivity.this ,MyAccountActivity.class);
i.putExtra("googleLogin", googleLogin);
startActivity(i);
//myaccount part
if(googleLogin){
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct != null) {
String personName = acct.getDisplayName();
//System.out.println(personName); working fine
account_name.setText(personName);
String personEmail = acct.getEmail();
//System.out.println(personEmail); fine
account_email.setText(personEmail);
account_mobile.setText("+91 1234567890");
// System.out.println(googleLogin);
// System.out.println(fbLogin);
}
}
solved this problem.
the issue was gmail was not able to provide account details for use when i tried to display them in MY ACCOUNT activity that i integrated in my app, thats why it kept on getting crashed. the code was correct thats why it wasnt giving any error i even tried logging it. there it showed correct details but still it wasnt displaying the result in text views.
so what i did was use shared preferences. everytime the user logs in using gmail, the details are stored in them and i retrieve it in MY ACCOUNT activity and yes it displayed the results correctly.
let me know if you need any more help.
I want to close both current and previous(parent)activity when user logout from app,
But Here the problem is that when user logout from app then it redirect to login activity to ask user for login, and if user press back button then it redirect to welcome page, how to close current and previous (welcome) page at once when user get logout .
here is my logout code in menu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Logout_secondActivity) {
session.logoutUser();
finish();
//Welcome.this.finish();
Toast.makeText(Second_activity.this, "Logout...", Toast.LENGTH_LONG)
.show();
}
}
Here is session manager for Logout user!,
/**
* Check login method will check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// if user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
Here i am already use flags
You can put shared preferences boolean value from current activity and then always check if a key exist in the parent activity (check it in onPostResume method). If the key exists finish this activity and if not, just remove the key. There might be simpler and more elegant solution but this one will work for sure.
You should start the required activity with startActivity after adding these flags. These will clear the activity stack and start the new Activity as fresh one. Use your required Class name in place of Splash.class and place this code after you logout your user.
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
EDIT:
What i understood from your problem is that you want to go to HomeActivity from LoginActivity either if it is in activity stack or not. one way you can do this is to override onBackPressed in LoginAcitivty . i.e when back is pressed in LoginActivity it starts the HomeActivity. You can do it like:
#Override
public void onBackPressed() {
Intent i = new Intent(this, HomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish(); // it is optional if you want to keep the login activity in stack you should remove this line
}
call finishAffinity(); after _context.startActivity(i);
for further reference you may follow this
https://developer.android.com/reference/android/app/Activity.html
UPDATE:
if you are calling this function from non Activity class then you must pass the Activity's Context when you call it, like this _context.finishAffinity()
I am working on a messaging app, it sends user notification when he is on a different activtyon my app or is on another app but if the user is on MessagingActivity.java it just updates the chat history and does not send any notifications which is perfectly fine, but the problem arises when the user is on MessagingActivity.java meanwhile an email or something else happen user leaves the MessagingActivity.java open and checks that app if in the meantime a message comes user does not receive any notifications
public void parseRequest(Bundle extras) {
if (extras.containsKey("for") && extras.containsKey("recipientID")) {
if (Integer.parseInt(extras.getString("recipientID")) == M.getID(this)) {
switch (extras.getString("for")) {
case "chat":
if (isRunning("MessagingActivity")) {
Intent intent = new Intent("update_messages_list");
intent.putExtra("data", extras);
sendBroadcast(intent);
} else {
Intent resultIntent = new Intent(this, MessagingActivity.class);
resultIntent.putExtra("conversationID", Integer.parseInt(extras.getString("conversationID")));
resultIntent.putExtra("recipientID", Integer.parseInt(extras.getString("ownerID")));
M.showNotification(getApplicationContext(), resultIntent,
extras.getString("ownerUsername"),
extras.getString("message"),
Integer.parseInt(extras.getString("conversationID")));
}
Let me know how you are checking that your MessageActivity is Running i.e. functioning of isRunning("MessagingActivity") method. If you are setting any global boolean variable for checking this and making isRunning value false in onDestroy() method of that activity then, according to life cycle of Activity it is not called until your activity is finished i.e. in your case user just switching from MessageActivity to Mail .
I am by no means an expert, but you could just set a boolean variable by overriding the Activity's onPause() and onResume() events.
Simply set msgActivityActive to true in onResume(), false in onPause(), and change your call to:
if (isRunning("MessagingActivity") && msgActivityActive)
I have created an activity named activity_create_password which will create password for the app when the application is started on the cell for the first time and next time onwards it should show the activity named activity_insert_password. how can I achieve this I am not getting Please Help.
You have to use SharedPreferences to achieve this, your code should be something like
SharedPreferences prefs = mContext.getSharedPreferences("appName", 0);
SharedPreferences.Editor editor = prefs.edit();
Intent intent;
if (prefs.getBoolean("isInitialAppLaunch", false))
{
intent = new Intent(this, activity_insert_password.class);
startActivity(intent);
}
else
{
//First Time App launched, you are putting isInitialAppLaunch to false and calling create password activity.
editor.putBoolean("isInitialAppLaunch", false);
intent = new Intent(this, activity_create_password.class);
startActivity(intent);
}
You should use the SharedPreferences class to achieve this.
See the below link to use shared preference.
How to use shared preferences
i make a lock screen application.. i have some service that listen to the SMS.. when the SMS receiving command andro-lock then i display a lock screen that called by service:
here'e the service's code :
if (tempMessage[0].toString().equalsIgnoreCase("andro-lock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-lock", Toast.LENGTH_LONG).show();
openDatabase();
updateStatusL();
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
}
that code has works perfecly, but i have some problem when i try to unlock my phone.. if my service receive command andro-unlock then i must close (finish) that lockscreen.java.. i was trying many code and still not works.. what must i do to close the lockscreen activity when my service receiving command andro-unlock?? please help..
else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//what must i do??
//any solution??
}
Thanks for your help..
A possible solution to stop the activity would be:
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);
In LockScreenForm
onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
finish();
}
You could send an intent to your activity with an extra, and the activity could read this extra and, if present, finish() itself.
Regards,
Stéphane