Clearing an activity stack via Intent is well documented with questions like this.
However this solution assumes that you are moving from one activity to another. Is there a way to clear this stack programmatically without having to start a new activity using an Intent?
To put this question in context:
My app has a database that synchronizes with a master online database. However the app needs to be available offline, so it has a completely replicated database stored locally in SQLite. I have an activity called FullSyncActivity which basically has a button called Start Sync. When the button is pressed, the sync starts (clears existing data and re-downloads from the server) and once the sync is complete, a new activity starts again.
The problem I have is that the user might navigate to the FullSyncActivity without fully understanding it's purpose - hence why I do not want to start the sync process when the activity opens (a Start Sync button is required). However, I cannot clear the activity stack when the activity is started (using an intent), because if the user cancels without pressing the Start Sync button, then they will have nowhere to return to. Ideally I'd like to clear the activity stack once the user presses the button. Is this possible?
I would do this by using a broadcast Intent. Have each activity register a receiver for a specific broadcast Intent. When the user clicks the Start Sync button, it should broadcast this Intent. When the receiver gets the Intent, it just calls finish(). In this way you can ensure that all activities end themselves when you send the broadcast Intent.
Related
I want to close the application gracefully. I found two methods.
1. Using Intents:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Documentation says:
Activity Action: Start as a main entry point, does not expect to receive data.
Category Home: This is the home activity, that is the first activity that is displayed when the device boots.
2. Using Finish:
finish()
Documentation says:
Call this when your activity is done and should be closed.
What is the best method or professional method for closing the Android application? Both close the application but finish() removes the app from cache (App not in recent activities) while using intent, cache does not deleted. Should cache remove from the cell on finishing the activity?
The best way is the proper one, as it's said in the official doc :
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
The App is a succession of activities. Closing the app means closing the first activity of the app.
The first method you described in your Post (1. Using Intents) seems to be a workaround; it's similar to when the physical home button of your device is pressed. It lets the activity to the back stack and launch the Home (First) activity of the device.
I launch another app from my application.
Is there any way to trigger my app when launched app closed?!
Is it good idea to use timer and check package name ?!
Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result.
Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another Intent object. Your activity receives it in the onActivityResult() callback.
More info here.
If you launch the app with startActivityForResult, you will end up in onActivityResult in your application when it closes, that is the normal flow.
But if you want to always launch your app when the other closes, it can be done using a service, but I wouldn't recommend doing that since it's bad practise.
I am using PendingIntent to take user from push notifications to Activity B i,e when user clicks on notification it will go to screen B.
I want it to be screen B only when application is loaded and running, else it should go to screen A. I have implemented this as well.
Now, there is scenario, wherein I have received the notifications, hence the intent is set to Screen B, but I haven't clicked it. If I kill the application process using DDMS or if the android kills the application before I click on notification and then I click it, it tries to load activity B which is not what I want in this case. I would like to load activity A in this case which is first sceen of the app.
Please, suggest what to do !
It sounds like you're deciding whether to launch activity A or B at the time you post the notification, which, as you've discovered, will not work: once you submit the notification to the Notification Manager, you can't make changes to it.
The way to do this is to always launch the same activity, but have that activity potentially redirect the user to a different activity based on whatever criteria you want. For example, you could launch activity B, which then bounces the user immediately to A if the app is not already in the foreground. (You could also do this with a third activity whose job is simply to route the user around, or with a service, or with a broadcast.)
Now the decision about where to send the user is made at the time the user taps on the notification, rather than at the time the notification is posted.
I am currently developing an android app which uses the android web browser and notifications.
What I want to be able to do is the user clicks on an item which loads the android web browser and makes a notification in the notification bar as well.
The user should be on the browser when they go the notification, so when the user is on the browser and they click on the notification I want the notification to perform its task but not redisplay the app activity instead just return to where the user was on the browser.
I've tried setting the different flags on the activity but none of them seem to make any difference.
Thanks for any help you can provide.
According to the following quote from the Android documentation, it sounds like there isn't a way to do some kind of background task without starting an activity when a user clicks a notification. Although you may want to look into sending an Intent to a Service. I've have not tried that before so I can't say whether it works or not.
A notification always starts an
activity as a new task (that is, it
puts FLAG_ACTIVITY_NEW_TASK in the
intent it passes to startActivity())
The documentation also suggests you could have a dedicated activity that could perform the copy paste, and then the user could simply press back to get back to the browser:
For example, when the user receives a
Calendar notification, choosing that
notification starts a special activity
that displays a list of upcoming
calendar events — this view is
available only from the notification,
not through the Calendar's own user
interface. After viewing this upcoming
event, to ensure that the user
pressing the BACK key will return to
the activity the user was in when they
picked the notification, you would
make sure this dedicated activity does
not have the same task affinity as the
Calendar or any other activity. (You
do this by setting task affinity to
the empty string, which means it has
no affinity to anything.)
To get round this as I did not want to have a service running in the background to perform this simple task what I have done is called the method at the end of the activity that the notification calls.
I call moveTaskToBack(true); which places the task into the background. As if the user presses the home button.
I have an activity which is declared in my manifest with android:noHistory="true", and 90% of the time, this is the desired functionality. This particular activity doesn't need to be saved in the application's history, and the application's UI flow is greatly complicated when it is in the history and needs to be manually removed by calling finish() every time the activity would otherwise be added to the stack.
The problem is, that this activity has the ability for the user to send an email, which is of course accomplished by creating an Intent with the ACTION_SEND property. When the user presses the "back" button from this activity, the next one to be shown in the stack is actually the one underneath my activity. Is there any way that I can force Android to add my activity in the history stack before presenting the ACTION_SEND intent?
I look forward to be proven wrong but I believe that the history stack is updated when the activity is created. At first I would suggest the rather hackish solution of launching a "proxy" intent that would be pushed onto the stack which could then call the e-mail intent and would be smart enough to launch the proper intent on the way back.
I'll update my answer if I find something in my notes.