Activity behind the passcode is shown in the activity stack - android

I have two activities - one for passcode and the other one behind the passcode activity. The flow is usually when the app is launched the passcode screen/activity is shown first and the main activity is shown next. But, when the app is on the main activity (not the passcode activity), and the app is minimized, the main activity is put in the stack. At this point, if the "recent list" is launched, the main activity is shown, not the passcode activity. Is there any way this can be solved? Or, is this how Android behaves?
Can someone please help me with this?
Thanks in advance.

Yes, this is how Android behaves, but there may be a solution.
If I understand you correctly, you want the user to always have to enter the passcode before seeing the main screen, whether when launching the app initially, or any time they leave the app and return.
For that, one approach would be to use one Activity with two Fragments. Whenever the Activity is restarted, load the passcode Fragment. When the correct passcode is entered, switch to the main Fragment.

This should work for you. You can use the following code in the main activity
#Override
public void onPause(){
super.onPause();
// put your code here...
this.finish();
}
This will remove the main activity fromthe stack every time the app goes in the background.
Also make sure that you don't finish() the Passcode Activity when you startActivity to the main activity.
eg: (in your Passcode Activity)
Intent i = new Intent(PasscodeActivity.this, MainActivity.class);
startActivity(i);

Okay, so... How about launching the passcode activity? Then from there you'd be able to launch the main activity, and finish the passcode activity. This way the main activity isn't launched until the passcode is actually entered, and it'll help you keep the responsibilities separated. Seems like the easiest solution to me.

Related

using onresume to redirect activity after pressing back

I have an activity that I use when my android app is opening to validate information about the user logging in. There is not a GUI for this Activity and it's intended to redirect to other Activities based on validation information. Somehow my app will go back to this screen and just be stuck on an empty Activity.
I have not coded an onResume.
Would I use onResume() as well as removing it from the back stack to prevent this?
Thank you!
when you successfully Validate user information in first "Activity" before going to 2nd activity ,call
finish(); //this will remove previous Activity from the stack
hope this will resolve your issue
It's absolutely not a good idea to define an activity for validating things.Just to answer your question no its not necessary to code onResume().onResume() is just a method which is called after onCreate() in an activity life cycle.
You can finish() the activity right in your onCreate() to prevent it from loading on press of back button.

launching already launched activity from notification creating problematic activity

i have 3 activities A-B-C .Lets say i am on activity B and from a listview there i launch activity C now if a notification comes which has an intent of launching activity C.
So the problem is I am getting multiple instances of activity C
When i use launchMode singleTop or instance or task the problem of multiple activity instance is solved but the newly launced activity does not work properely as it is desired to be.
Please help me out tired of using flags and stuff but not able to overcome this problem.
The scenario is Just like whatsapp , if u r talking to person one and a message of person 2 come as notification ,when u click on that notification the activity relaunches and works properely. This is exactly what i want. Please help me out how to achieve this . :(
Thanxx in advance
What Flags did you try and what exactly is not working, means, how does the Activity behave?
What you describe with WhatsApp could be achieved with two steps:
Use the FLAG_ACTIVITY_SINGLE_TOP for the Activity.
Overwrite the Actvity.onNewIntent(Intent intent) method in the Activity. This method is called instead of creating a new Activity and gives you the new Intent. I assume that the Intent contains all necessary information in the extras.
The user experience should be as follows:
User chooses from the list, which opens the new Activity
He/she can press home or back button from here. This brings the home screen or the list back.
If, for any reason, somebody else calls startActivity for your Activity while it is running, the onNewIntent is called. There you can add code to refresh the content, maybe have an Alert that tells the user that the content has changed, and shows all that to the user.
If the user presses back or home now, he/she will get to the list or home screen
If that is, what you're looking for, then the flag and the method is what you need

Avoiding login activity on startup if password is cached

I have a typical Android app where the user has to log in before displaying the main Activity of the application. However, if we've already got a cached username / password, I want to skip the login page.
I could, in my Login onCreate, detect if I've got a user/pass and push a new Activity, but I'm worried that this will make my app startup slower (since I have to load an activity then immediately throw it away), and it also perhaps breaks the back button (i.e. you can hit back and end up back at the login screen).
Is there any way to avoid this and only load the full Login Activity if there is no cached password?
You can make a separate helper activity which launches either your login activity or your main activity. In its onCreate, you'd use startActivity and immediately call finish to remove the helper activity from the back stack.
Intent intent;
if ( /* already logged in */ ) {
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, LoginActivity.class);
}
startActivity(intent);
finish();
Then, LoginActivity should re-launch MainActivity as normal. This way, the user will never be able to navigate back to the helper activity as it does not appear in the back stack. Do note however that the user can still login, go to the MainActivity, pause that activity, remove his account (through the Android settings) and resume the activity (from the recent apps). If you want to prevent this, you're probably better off placing the login redirect in MainActivity instead (perhaps even in onResume).
You could create a Splash Activity.
On the AndroidManifest.xml's Splash Activity tag, add the android:noHistory="true".
On the Splash Activity, check whatever you need (maybe with an AsyncTask if it may take a long time, to avoid freezing the Activity) and, depending on the result, you start the Login Activity or any other Activity.
Another approach is to transform login screen from Activity to Dialog. That should be easy. And then make your main activity check if there is cached username/password or not. In the second case show LoginDialog to user.
I also have a typical Android app with the same requirement, and I solve it as follows.
First I configure the initial launch activity to be MainActivity, and then inside onCreate(), check if the user has saved credentials (this is all done through AccountManager and authenticator service).
If the check fails and user needs to authenticate, then I start the LoginActivity. If logging in fails or the user presses back, the MainActivity calls finish() on itself to close the whole app. Otherwise, after logging in, the MainActivity is resumed and is presented to the user properly.
The advantage of doing it in this order is that (in my case anyway) the user is more likely to be logged in than out, and so this will avoid any start/stopping of unneeded activities as you say.
A second advantage is that, if the MainActivity (or another activity) is resumed at a later time and the user's session has expired, you can simply start the LoginActivity again to reauthenticate them.
Hope that all makes sense :)

clearTaskOnLaunch but considering this

Sorry for my english.
My "Root" activity (let's call it Activity A) is an activity that asks for a password. If the password is correct it should start another activity (B) with subactivities and so (B, C, D, etc).
When the user presses "back" in the Activity B, the app must exit (and not back to Activity A). To achieve this, I simply "finish()" Activity A after I call Activity B. This process works OK.
The problem is when the user presses "home" and then starts the app again, the app resumes from the last point and not from the Activity A.
I use 'android:clearTaskOnLaunch="true"', but it is not working because the "Root" activity is killed when the password is correct.
What is the best solution for this situation?
Thanks!
There are some tricks I can think of, but people will still be able to get to it through Recent Tasks. You should perhaps rethink your logic of when the user needs to re-enter a password.
You can try merging Activity A with Activity B.
At the first, make the Log In controls visible. After you had validated the user, make the login controls invisible, and make all the other controls visible.

How would one design a flow where one of the screens is a login screen?

I am writing an Android app which requires the user to be logged in. I have a main Activity, and I want that when the app launches, that it takes the user to a login screen if they are not already logged in.
How should I model this? Is the login page another Activity? I'm thinking not because I don't want the user to be able to get back there using the back button. Or is there a way to replace an activity with another so the back button problem doesn't happen?
Or, should I have just one activity and load in a login view and swap it out once the user logs in?
Are there any best practices around this?
Thanks!
I would check on the main or splash screen to check if the user is logged in, if not start the login activity.
Once the login completes, in the login activity, call this.finish()
If you need to change activities, you can call .finish() followed by starting whatever activity you wanted.
If getting back to the splash/main screen without being logged in is a problem, you can do the same thing there.
For this ask "I'm thinking not because I don't want the user to be able to get back there using the back button"
The answer is using the attribute noHistory in your Login Activity, like the example:
<activity
android:name="com.test.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:noHistory="true"...
I hope it can help.
Yes, the login screen is an another activity. The back key problem is solved with onKeyDown() method of that activity. Read more here.
The whole flow can be imagined like this:
You're starting your app. The main activities check for login flag(in shared preferences or somewhere else). If user has logged in, then the main activity stays on the screen. If not, activity starts login activity using intent. Login activity performs the logging in and sets the login flag and start the main activity again.
Have you considered using a dialog for the login? I suppose what you could do is have the first Activity check at onCreate() using SharedPreferences if the user has already logged in, and if she hasn't then it generates the dialog. After logging in the dialog would change a variable which would be passed to SharedPreferences so that the user won't have to relogin if the screen orientation changes or the app pauses.

Categories

Resources