开发者

How to use intent between tabs in java/Android?

开发者 https://www.devze.com 2023-01-03 08:02 出处:网络
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 iclick that text view it

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?


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.


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


I dont see the Problem here: Maybe its a little bit of hackish but following Code works for me:

    public boolean onClick(View v) {
    //get your data you wanna send. 
    //If it is an Object it would be good if it is Parcelable
    Object o = getYourData();
    // or Parcelable p = getYourData
    Activity activity = getParent();
    //I'm assuming were inside an Activity which is started by TabActivity
    if (activity instanceof TabActivity){
        TabActivity ta = (TabActivity)activity;
        //now determine the Tab you wanna start
        ta.getTabHost().setCurrentTabByTag("yourTag");
        //or ta.getTabHost().setCurrentTab(yourID);
        Activity current = ta.getCurrentActivity();
        //check if the Activity is the one you wanna start
        if (current instanceof YOUR_ACTIVITY_YOU_WANNA_START){
            //Cast to your Activity
            YOUR_ACTIVITY_YOU_WANNA_START yourActivity = 
                    (YOUR_ACTIVITY_YOU_WANNA_START)current;
            // you only need to put Data inside your Intent
            Intent intent = new Intent();
            intent.putExtra("EXTRA_DATA_TAG", o);
            //your Activity must Override onNewIntent and make it public, 
            //or simply add another method
            //with whatever You like as parameter
            yourActivity.onNewIntent(intent);
            return true;
        }
    }
    return false;
}

this way you don't have to mess with Application, SharedPrefs or other unnessesary stuff mentioned here


If you make the intent you are using to start the second tab activity a global intent.

You can then add extra's to this intent in the onPause() of the first tab. Note: you have to define all your tabs in separate activitys than your tabhost TabActivity as this activity's onPause() is never called.

This also help's with the answer above, if you are using a global variable saved in your activity that extends application, you can set this in the onPause() as it is fired before the activity is switched, which you might find an issue if setting this elsewhere

0

精彩评论

暂无评论...
验证码 换一张
取 消