Do not show mainactivity again until app fully restarts - java

I have an application which has a main activity. When it switches to a second activity I do it like this :
startActivity(intent);
finish();
I was hoping this would fully prevent the application from going back to the main activity. When I press the android back button in the secnond activity it goes back to the home screen (which is what I want). But when I open the app again from multitasking it goes back to the first activity! Has anyone run into this issue before?

I was running into this problem, where my first screen would be a login activity that's only meant to show if the user is not logged in. Otherwise, it show the home screen. I tried out SharedPreferences for login persistence, but it turns out the authentication service I'm using, Google Firebase, has built-in login persistence.
Without more information, however, I can only suggest that you research SharedPreferences and have a boolean check a condition upon start-up. If the condition is met, start the home activity; otherwise, show the main activity. Something like:
boolean isLogin;
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
isLogin = settings.getBoolean("islogin", false);
if(isLogin) {
finish();
goToDash();
}
Public Interface Shared Preferences, Android Developers
Obtaining Shared Preferences
Working with Android Shared Preferences

Figured it out! My application uses a twitter api, and relaunching the application would cause an error as it retried to get credentials it didn't have before. I also made sure to completely clear the stack when switching certain applications by doing:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);

Related

How can I prevent a user from pressing the back button, because if the user does it closes the application and restarts it from the first activity

Hey guys I'm currently developing an application for my final project, the concept is a contact keeping app but with user login and registration. When the user enters their credentials and presses the login button it starts an Intent to move to another page, on this Intent I end it with the .finish() method so the user can't go back but when that is executed and lets say by accident the user presses the physical back button on the device the application will close and if you try to open it again by going on the multitask physical button on the device and you select it, it starts the application again from the beginning (the login screen) how can i make it that if the users presses it by accident they can open it again from the multitask or the physical icon of the application so the it picks up on where it left at (the display activity after you login it) essentially not restarting the application.
Is that even possible?
Thanks in advance
Override onBackPressed() in your activity and remove super.onBackPressed().
#Override
public void onBackPressed() {
// super.onBackPressed();
}
First of all to stop device default back key event you have to remove super.onBackPressed() from onBackPressed() of activity
#Override
public void onBackPressed() {
// super.onBackPressed();
}
Now to keep the flow management after login, you have to save the activity name or any other value to SharedPreferences so whenever you come again to the application you will make a check for last activity after login. And navigate to the same.
SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
//add this to onCreate of the activity where you want to come directly
editor = editor.putString("LAST_ACTIVITY", "MULTITASK_ACTIVITY/*Navigated activity name*/").commit();
Now make a check on app start in loading screen:
if(preferences.getString("LAST_ACTIVITY", "").equalsIgnorCase("MULTITASK_ACTIVITY")){
//Navigate to Mutitask activity
}else{
//Navigate to other activity
}

android - resuming from last visited page in my app

I have login page (activity) which is my first activity (landing page) of the app.
Only after user gives correct username and password, the app allow you go to other pages to work with.
Now my question is, now the user is at the fourth page and he is working on it, And he minimize the app and use other app like messaging etc..
so when he come back to click my app icon again, my app starts from the first landing page, which is the login page,
how can I make my app to resume from the fourth page?
Android 4.4.4, Kit Kat, Samsung
Regards,
Nay TK
Add this in android manifest of loginActivity,
android:launchMode="singleTask"
more on android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
go through official link for more detail
You can make your fourth activity as your launch activity.
In the Resume of your Activity, you check if the user is logged in. If not, you start your Login activity.

Check whether an activity is active or not from a different activity

I have a flow in my application like this:
For new users:
Splash Screen --> Login Activity --> Home Activity
For already registered users:
Splash Screen --> Home Activity
Basically the Splash Screen has an if else to decide which activity to go to. Once a first time user logs in, his status is saved in a preference variable for the splash screen to decide next time not to open the login activity.
Now the situation is that. If a new user logs in and goes to the home activity, and then logs out. He is redirected to the Login screen which is pretty much what should happen. But, in case an existing user opens the app, he is shown the Splash screen and directly moved to the Home Activity. Now if the user logs out, he gets out of the app. This happens because the Login Activity does not have any instance created and thus finishing the Home Activity finishes the whole app. Logout actually finishes the Home Activity, naturally the last active activity should open up. Which is not happening.
What I want to do is that, I want to implement a logic which will check that the Login Activity is available or not. If its available then finish() will be called else the Login Activity will be called via intent.
Please tell me how to achieve this.
P.S: My app uses a custom theme with a customized action bar. If I call finish and Intent together or I use flags to clear existing activities then there is a weird transition effect which shows the black standard action bar for a split second thus creating a bad user experience.
Now if the user logs out, he gets out of the app. This happens because
the Login Activity does not have any instance created and thus
finishing the Home Activity finishes the whole app.
If i understood your question, why dont you just call the Login Activity manually after user click a logout button?
Its what i always did with apps that have flow like yours
when user login finish login activity and start home activity.
when user logout finish home activity and start login activity
You always can call Login Activity via intent. If the activity is available, android will show this activity. Else android will create new activity automatically.
Actually that's why we use intents to show activity instead of creating activityes manually. System catches this intents and does all dirty work.
EDIT:
Hmm, but wouldn't you have the transition problem anyways? (If you were already logged in, and then log out - using intent/finish() you will have the same black action bar issue no?)
Maybe consider following ( I actually did this in my app):
Merge Splash screen and Login into one activity and depending whether you are logged in - display the login fields or proceed to home screen. Then you have a out of box consistent stack of activities regardless of use cases and no mambo-jumbo with do I already have this or not.
I can't comment because of I lack 4 rep, so I'll post as answer here:
I think #Blaze Tama is right. You can also use FLAG_ACTIVITY_CLEAR_TOP on intent to avoid stack flow problems:
From docs:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Always start Login activity and start Home activity right away if the user already logged in.
In the Splash Screen activity
Intent intent = new Intent(this, Login.class);
If (user already logged in)
{
intent.putextra("Logged in", true);
}
startActivity(intent);
In the Login activity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null)
{
if (intent.getBooleanExtra("Logged in", false))
{
startActivityForResult(new Intent(this, Home.class), requestCode);
}
}
else
{
// The existing code here
}
}
In Home activity send back a code to indicate if the user logged out or just BackPress. If BackPress finish this Login activity.

bringing up different page when logged in

let me start off by saying happy holidays to everyone!
ok i really just need confirmation and correction if needed.
what im trying to do:
Im using google and facebook "Log-in" feature to sign in for my app to retrieve the data needed like name email etc.
where i Need help:
after signing in i want another activity to be the forefront everytime app opens unless the user signs out then of course, it takes them back to the original main page to sign back in.
now im assuming this takes place in maybe the lifecycle right?
somthing like:
#override
OnResume
{
//if user is signed in cast an Intent to automatically go to another activity?
}
am i on the right track on no? thanks in advance guys
I'm not sure off the top of my head how Google and Facebook's login is implemented... do they have a sample project you're using?
And yep, you're on the right track! Generally speaking you should be able to have a "Main" activity (MainActivity for this example) which checks to see if the login was successful, and if so, kicks you to the activity you want (LoggedInActivity).
This would be in the onCreate() or onResume() method of MainActivity.java
onResume() { // onCreate() should work, too.
if (loggedIn) {
startActivity(new Intent(this, LoggedInActivity.class));
} else {
// send them to login
}
}
If there's not a good way to check if they're logged in, you could save a boolean value or api token using SharedPreferences once the login is successful, and check that value (that'd be the value of loggedIn) every time at launch. You'd obviously need to clear that value any time you logged out.

Staying 'logged in' even when app destroyed

Hi I have an App which has a strict requirement of appearing "logged in" even following the app being destroyed. If the App was destroyed then later re-loaded (if previously logged in) login screen should be bypassed > directly to the page the user was previously viewing. (I already handle all session related variables)
I've tried using Shared Preferences but if I destroy the App manually it would go back to the login screen, I can't have this happen.
I tried storing preferences manually in the DB (which is probably what Shared Preferences does anyway?) but it still loads the Login Activity of course because this is my first loaded activity. So this problem is more a case of keeping a preference on which Activity the user is on at all time then loading directly to this onResume().
My question: Has anyone dealt with this sort of scenario before? How should I approach pre-loading the Activity. I was thinking that when I load my preferences within the Login screen I check the previous Activity preference and simply load into that (assuming user is logged in).
Is there a better way to approach this? Is there a more native way of loading dynamically to appropriate Activity start?
Any help is really appreciated
Edit: Ok just after posting this I realised all I really had to do was add a check in OnResume whether the appropriate session variable was set. then load into the Activity, actually just as Akbari says below. I've rolled my Preferences class back to using the standard Android SharePreferences and its working perfectly now. happy about that :)
Simply doing something like this:
// load preferences
preferences.readPreferences();
// if preferences exist load straight to RecordActivity
if (application.currentSessionId!=null) {
Intent i = new Intent(getApplicationContext(), RecordActivity.class);
startActivity(i);
}
you can save login status in preferences and check it in onCreate() method of your Login activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);SharedPreferences prefs;
prefs = getSharedPreferences("your_pref", MODE_PRIVATE);
boolean login_status = prefs.getBoolean("login_status", false);
if (login_status) {
Log.v(LOG_TAG, "UserInfo>>User already logged in");
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish();
}
}
Here, it will check login status and redirect user to next activity if already logged in

Categories