David K.
David K.

Reputation: 6371

android intents and callbacks

I'm going through the tutorials for android and something about intent/activity interaction is confusing me. In Javascript whenever there is an ajax call we define how the results should be handled along with the ajax call and we can use different callbacks for different ajax calls throughout the application lifecycle. In android starting an activity with an intent and handling the passed back results are decoupled, at least that's how it's done in the tutorial and there is only a single point of entry for how the results are handled so it's hard to perform on the fly handling of results without messing with the main entry point. I can easily imagine some complex logic that could make the switching inside the main entry point into a horrible mess. Is this a fundamental android architectural thing or is there another way to do things with actual callbacks instead of switch statements in a single entry point?

Upvotes: 2

Views: 6384

Answers (3)

Iftah
Iftah

Reputation: 9572

Also note that you do not have to start a new activity for a background process without its own screen such as an Ajax call - you can use AsyncTask instead which allows for a javascript style callback.

Upvotes: 1

Cheryl Simon
Cheryl Simon

Reputation: 46844

It is true that you are limited to a single location for receiving responses that an activity has finished. It would be nice if you could define a callback function for each, but that is not how it works.

In my experience though, you seldom have so many different destinations from a single activity that it is hard to manage. Generally each page only leads to one or two other pages that you might care about getting results from.

You can do something like the following to cleanly separate your logic for each case:

void onActivityResult(int requestCode, ....) {
  switch(requestCode) {
     case Activity1:
        onActivity1Result(...);
        break;
     case Activity2:
        onActivity2Result(...);
        break;
  }
}

Upvotes: 3

Nick Campion
Nick Campion

Reputation: 10479

Intents and Activities are designed to allow developers to develop re-usable, loosely coupled components.

I understand that, when working internal between two activities that you are creating, the mechanisms can seem unnecessarily restrictive. The restrictiveness is part of the open nature of the platform. The same mechanism that you use to start an activity you own could start an Activity created by another developer or by the OS itself.

That being said, there are a plethora of options for passing information between activities. It really depends what you are trying to accomplish. I try to think of activities just that, activities from the users perspective. I'm going to list some mechanisms for passing data and, if you'd like to further describe your application or need, I'll try to help you narrow the options down:

  • Intent.putExtra
  • startActivityForResult (I'm assuming you know this one)
  • SharedPreferences
  • Service
  • ContentProvider

Upvotes: 1

Related Questions