android launchMode singleTask with startActivityForResult - android

We have a requirement to force the user to set up a password the first time he/she starts the app. So we have our main activity with launchMode=singleTask which start a password setting activity using startActivityForResult.
We also want the user to go back to their home if he/she taps back from the password setting page so we put the code to finish the main activity in its onActivityResult if it receives RESULT_CANCELLED. However, if the user taps home and re-enter our app, we want to show the password setting page again. But in this case, it will be destroyed (because main activity's launchMode is singleTask) and also returns RESULT_CANCELLED to our main activity causing it to be finished.
So the problem is from main activity, we cannot distinguish between tapping back and tapping home then re-enter the app.
Is there anyway to fulfill this requirement while still keeping the launchMode as singleTask ?

onBackPressed() for tapping back
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29
&&
onPause() for home screen
(though it can be due to other things also like your next activity or switching to another application)
http://developer.android.com/reference/android/app/Activity.html#onPause%28%29
Update :
Why Don't you use Sharedpreferences and check if the app is run for the first time and password/rest fields are set rather than using the approach you are using right now .

I had a very similar issue recently.
What I would suggest is that you don't actually finish the app when you get the RESULT_CANCELLED.
Instead do something like:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_CANCELED) {
if (/*TEST IF A USER IS SIGNED IN*/) {
//IF SIGNED IN PROCEED TO MAIN ACTIVITY
}
// OTHERWISE FORCE THEM TO SIGNIN/REGISTER/WHATEVER AGAIN
// WITH ANOTHER CALL TO startActivityForResult
...
This is a back of an envelope approach to your problem, but this pattern has worked for me.
For me, my Authentication/Registration is all done in an Abstarct Class that extends Activity. Each of my activities then extends this class. I Place the Auth/Reg method calls in the onResume() of this Abstarct Class.
When a startActivityForResult is launched in the onResume() method and fails I don't finish() the activity but just let the app proceed and let the class handle which Reg/Auth form to present. Which it will do by checking various flags to determine what action it is meant to be performing.
The added benefit of this approach is that each and every Activity that might extend this abstract class gets the added benefit of a security check each time it is used.

Related

Remote finish an activity called by intent-filter?

I have 2 activities: A and B. A calls browser intent via Intent.ACTION_VIEW and after user logins in browser he's redirected by intent-filter to activity B.
The problem is: When user gets to B, activity A must finish.
I know about startActivityForResult(), but I do not directly call A->B, it goes A->browser->B, so it doesn't work.
I can't use onStop() and onDestroy() methods in A however, because app would crash on orientation change.
What shall I do?
When user gets to B, activity A must finish.
Why ?
Close it, if user comes back from B ( or browser )
I never had the problem, that a activity must finish. Maybe you can explain the situation a little bit more, so we can understand better.
BTW: you can also lock the orientation programmatically, but I am thinking you are running in the wrong direction.
// hans
For Android API Version from 16 Use finishAffinity to close the Activity B as well as all activities immediately below it, so that the Activity A will also be displayed.
Just Override the finish method in Activity B
#Override
public void finish(){
finishAffinity(); // supported only from API 16
}
By default all the Activity will have same taskAffinity as the package name. If you want to close only specific Activities , then set a custom Affinity name for the required Activities
finishAffinity - Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task.

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 :)

Start root activity if no other activity live for this android app when press back key

I have a message notification, when user select the notificaton, a message display activity will show.
When user read the message and press back key to close the activity, I want to check if the previous activity is the same app's, if so, just go ahead, if not, I want to start the home activity for this app.
How can I do that?
How about this: Have the notification launch the Home Activity in a state that immediately has the Home Activity launch the MessageDisplayActivity. That is: (1) The intent that the notification sends should target the HomeActivity and include an extra flag identifying the Intent as being from the notification. (2) The HomeActivity, in onCreate(), upon finding that flag then simply launches the MesssageDisplayActivity.
Thus the HomeActivity will exist for a brief moment of time, but probably not long enough to be visible. It will then remain upon the back stack so that the back key will bring you there.
You should consider changing the launch mode of your Activity, so that you never have this problem - using singleTask or singleInstance should make this Activity always be the only one open for your app.
Have Intents within your app that call the message display activity (MDA) pass a boolean value of "true" that's extracted and stored in a field in the MDA. Have the field set by default to "false." Thus, if the MDA is called from an activity within your app, the field will have a boolean value of "true," if it's called from anywhere else (such as a notification), it will be set to false.
Override public void onBackPressed() in the MDA as follows:
public void onBackPressed() {
if (wasCalledFromYourApp == true) {
super.onBackPressed();
} else {
//[code that launches your app's home activity here]
}
}
(This is assuming the notification will only be created by one app).

Android, Finish all activities

I need to finish all the activities running in an Android application when an exit button is clicked. How can I do that?
What I want:
An option menu on screen with an EXIT option.
When I click the Exit menu, the application should close.
How to make a button for the user to immediately finish all Activities
When the user wishes to exit all open activities, have them press a button which loads the first Activity (passing in an intent to clear out all the other activities) that runs when your app starts. Then inside the one remaining activity (LoginActivity), place some code in onCreate to have it choose to self destruct.
Details:
Create a button and call it "exit", make a buttonlistener for that button, and put the following code in there. What it does is load the activity, makes it the only remaining activity by clearing all activities underneath it.
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities under LoginActivity. The LoginActivity must be the first activity your app runs. So now we are left with only LoginActivity activated. Now we have to get LoginActivity to check the putExtra parameter, and if it has the value "EXIT" then destroy itself as well.
Put the following code inside the LoginActivity's onCreate and onResume, to check the "EXIT" parameter and finish when it is seen:
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Why is this so hard, why isn't there just an exitAllActivites() function?
Android OS Developers make this hard because they don't want you to give the user an Exit button. They want the user never to care if their App is running or not. If they want it, they run it, if they don't want it, they stop using it. There are benefits to this model, because then an app can be destroyed to make room for a phone call and heavy map-usage, and then they re-run your app, they pick up right where they left off, rather than having to restart from scratch.
So if you use this code, try to cooperate with the Android Developers vision for Android and make it so that the App can be destroyed/recreated at any point, and the user always picks up where they left off. Requiring a user to 'exit' is a hassle.
use StartActivityForResult function for starting a new Activity and when user presses EXIT button, send a unique value as a resultcode. Check for this resultcode in onActivityForResult func and if you find the same unique code then finish the activity.
I don't understand the people who say that this should not be done. Closing an activity can be done quite easily using the built in method to do so.
There is no place in the official documentation that says this should not be done.
For those who think it should not be done for any purpose, maybe you can't think of a reason to do so. There are plenty of legitimate reasons to do this. And since it is a method built in to the Android code.. Google has also decided that you might need to depending on your use. Not all of us only create consumer applications for Android.
So to accomplish this task..shutting down all of the activities in your stack
public void quitApp(){
this.finishAffinity();
}
I created a kiosk app that was run as the default launcher. So I needed a way to exit the app to get to settings or other apps on the device. So in an admin Activity, I placed a pin number pad.. after the user inputs the correct pin, the app needed to exit to the original launcher. I used above code.
You should not implement an Exit button in an Android application.
Read http://groups.google.com/group/android-developers/browse_thread/thread/1bf0f7a4a9c62edd/a0a4aedf21ae5f76?pli=1
Cheers
How to close all activities in Android:
Why are the Android OS devs telling me not to create an Exit button?
If you give developers a hammer, they will use it, and everything becomes a nail, so don't give them a hammer. This is why there is no one-liner to exit all activities.
Why is an Exit button so bad?
You may be frustrated wondering why killing all activities in Android is so difficult. It's hard to do because you've got to bite the bullet and understand how you created the stack, and what it looks like. In what order should the Activities be destroyed? What if the user gets a phone call and needs to nuke your app ASAP? You NEED to have a plan for unwinding. It can't rely on the Activity Manager to babysit your app. If you want to kill your app, the OS devs are twisting your arm to create a unwinding plan because they don't want your application to have any errors when the Activity Manager nukes it.
This self destruct mechanism I go on to describe cooperates with the Activity Manager and causes a cascade effect that causes all activities to finish themselves no matter how they are organized.
To add this cascading self destruct to your app, do these three things:
If you are newbie to android and have never done this before, I suggest you create a brand new bare bones android project to test this out and get a feel for how it behaves. Only when you get the "aha" moment of understanding why this works, then can you extract the usefulness of the code below to delight the user.
Put this override method inside each one of your activities:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//onActivityResult is called when a launched activity exits, giving
//you the requestCode, 1234, that you started it with. The resultCode
//it returned, 900, and any additional Intent data from it. The
//resultCode will be RESULT_CANCELED if the activity explicitly
//returned that, didn't return any result, or crashed during its
//operation.
switch(resultCode)
{
case 900: //Make 900 a final global variable if you want.
//900 means CLOSE_ALL_ACTIVITIES to me. It is
//a signal that the self destruct button has
//been initiated and all activities should end.
setResult(900); //setResult sets the resultCode that will be passed
//to onActivityResult in the activity that called
//this activity. this will immediately tell the
//parent to self destruct even before the next
//command, finish(), is run.
finish(); //Now stop this activity.
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
If you have more than 3 activities, consider having each activity extending a "MyAppActivity" with the above code in there. If you catch yourself copy/pasting the above method into 15 different activities, slap yourself once, and take a class on good Object Oriented Programming. Then read this.
( Details about onActivityResult. ), ( Details about setResult. )
Whenever you have the user start a new activity, you must do it exactly this way:
Intent myIntent = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult(myIntent, 1234);
//The 2nd parameter of startActivityForResult is what is sent to
//resultCode in the onActivityResult method. The code `1234`
//doesn't do anything here because it is a flag that is
//ignored in onActivityResult.
More information about startActivityForResult.
If you don't use startActivityForResult, then the self destruct unravelling won't work.
When you want to exit your application, initiate self destruct like this:
setResult(900); //900 is the self destruct code.
finish();
Then the activity stack unwinding plan cascades through the entire app.
you should do this on yourself. read this good blogpost: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Another solution to finish all activities works for me:
Somewhere in my Controller class, there is a method to initiate the shutdown:
/**
* Ask for application shutdown.
*
* <p>
* After the call, the system shall wipe the entire task and activities and
* then call {#link #onTaskWiped()} to finish the cleaning.
* </p>
*/
public void attemptShutdown() {
// wipe the task and its activities.
Application app = getApplication();
Intent intent = new Intent(app, ShutdownActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
app.startActivity(intent);
}
The ShutdownActivity is just performing data cleanup before finishing itself like it was told before.
/**
* This activity is started by {#link Controller#attemptShutdown()} to wipe the
* entire task and activities. It calls {#link Controller#onTaskWiped()} in
* {#link #onCreate(Bundle)} and finish immediately after.
*/
public class ShutdownActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is where you release the application data.
Controller.controller.onTaskWiped();
finish();
}
}
And finally the Manifest part. I have set the transparent style to avoid the small flash when the activity appears but it does not seem to work well.
<activity android:label="Exiting..."
android:name=".ShutdownActivity"
android:excludeFromRecents="true"
android:theme="#style/Theme.Transparent" />

Android: Different start activity depending on user preference

My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.
What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.
Right now, in the Welcome activity, I read the preference and call finish() on the current activity:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean skipWelcome = preferences.getBoolean("skipWelcome", false);
if (skipWelcome) {
this.finish();
}
And then I implement onDestroy to move on to the next Activity:
#Override
public void onDestroy() {
super.onDestroy();
startActivity(new Intent(Welcome.this, StartFoo.class));
}
But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.
I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.
Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),
"As a special case, if you call
startActivityForResult() with a
requestCode >= 0 during the initial
onCreate(Bundle
savedInstanceState)/onResume() of your
activity, then your window will not be
displayed until a result is returned
back from the started activity. This
is to avoid visible flickering when
redirecting to another activity."
Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning
if (skipWelcome) {
// Create intent
// Launch intent with startActivityForResult()
return;
}
The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.
There are a few solutions to this.
Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.
More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.
Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.

Categories

Resources