Cruces Cruces - 1 year ago 59
Android Question

How can you start a different activity in android according to the intent's data

I am making an application that will in fact contain two activities.

One will allow the user to log in and if done successfully will launch a web browser (MenuActivity).
The other takes a picture and uploads it to a server (PhotoActivity).

That web browser will have a link that when pressed will load my application in picture taking mode.

The flow is usually something like this:
MenuActivity->web browser->PictureActivity and then the last two steps repeat.

I have done this so far by setting my launcher activity as the MenuActivity and then putting using this code in the OnCreate override:

Intent mine = getIntent();
if (mine.getData() != null && !mine.getData().getPathSegments().isEmpty()) {
//retrieve the data here that I need
Intent i = new Intent(this, typeof(PhotoActivity));
i.PutExtra(//add here the data I received);
StartActivityForResult(i, START_PHOTO_INTENT);

after that I override OnActivityResult and send finish() if the requestCode is START_PHOTO_INTENT

This works correctly

my problem is that when I start the PhotoActivity from the web browser, if the user (while this is open) opens the application, then the MenuActivity shows (which it should) but when the user pressed back on it, it goes back to the PhotoActivity instead of exiting.

Is there a way to change this behaviour so that , even if the PhotoActivity is open, if the application is started manually, the PhotoActivity will not remain on the backstack ?

Thanks in advance for any help you can provide


After reading the linked article at the solution and the answers to this question I came to the conclusion that in order to do what I want I had to set LaunchMode as singleTask in androidmanifest, and override MenuActivity's OnStop and send finish() after it was executed (if I didn't and the activity was in the backstack then it wouldn't be fired with the intent data when launched from a web page)

thanks for the help all of you

Answer Source

Look at the Handling affinities chapter in Tasks and Back Stack documentation. It is described how to handle this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download