views:

211

answers:

3

I would need to know how to handle the intent between tabs. For example, I have a tab activity with two tabs. First on content is a text view. another one is a map view. When i click that text view it redirects to the tab2. it can be easily achieved by setCurrentTab(1) or setCurrentTabByTag("tab2") methods. But i want to pass lat and long values to that Map Activity to drop pin. What is the better way to use intents or getter/setter in java? What do you prefer? if your answer is "Intents". How?

+2  A: 

A interesting problem. I understand it that you want to change to the second tab on a click in the first tabview but also pass special data to the second tab that is dependent on the action in the first tab.

I would generally start your views inside the tabs with an activity. However this is done at the moment the tab host is configured. That means both intents the one for the activity that lets the user choose lat long and the one that shows lat long are openend at the same time.

Therefore you can't add the information to the intent used to intialize the tab host. I don't like the solution but the only thing that comes to my mind (using different activities for the tabs) is using a custom application that stores a reference to an object containing the data that you need to update the view in the second tab. You have to extend application with an own class and add this class in you manifest, now you can call getApplication in the first tab cast it to your application class set lat and long just before you call setCurrentTab. In the second tab you can call getApplication() again and you will then get the application object that is the same for every activity at every moment of your app running. You then have cast it again to your application object and retrieve lat and long value. See this page in the google refs on how to use a custom application class.

To use a custom application class add the following to your application tag in your manifest:

  <application
  ...
   android:name=".somepackage.CustomAppClass"

This will tell Android to instantiate the CustomAppClass as your Application class at the moment your app starts. You need to extend Application to avoid errors on start up.

Another solution but not the one I would prefer is to initialize the views yourself and initialize the tabhost with views and not activities. With a map view in one of the tabs this could be very memory heavy.

Janusz
you confused me a lot. i am using different activities for the tabs. when i click a view on the tab1. it redirect to that Map Activity(second tab). Main thing i am get that latitude and longitude from tab1. that value changes dynamically.
Praveen Chandrasekaran
that is the problem you can't use intents in this case because the intent to start the map activity is send at the moment you initialize the tab. this means that you can't attach data to it later. You need to have some global accessible variable for both activities that holds the lat long valuesI editet my post, it is still pretty complicated but maybe not that confusing anymore. I try to find a tutorial for a working with custom application classes.
Janusz
then i want to use getter/setter methods. right?
Praveen Chandrasekaran
i can understand your thought? struggling with implement that on the manifest. can you post some sample code?
Praveen Chandrasekaran
i edited in example code that shows how to add a custom application class to the manifest
Janusz
i have completed that using shared preferences. Thanks. But get the error on map Activity. that is, Couldn't get connection factory client. do you know why?
Praveen Chandrasekaran
The whole question will be much cleaner if you ask this in a separate question.
Janusz
+1  A: 

If you want to pass values between activities, I suggest looking at

http://developer.android.com/reference/android/content/SharedPreferences.html

the best way to get values from one itent to another.

With sharedPrefrences, there is only one instance of the class for the whole application, which means that you can store values in the files, switch intents or activities, and then recall those sharedPrefrence files that have the data in them.

The only downside is that you have to pass primitive types (int, string, boolean) but I'm sure you'll figure ways around this :).

hwrdprkns