5

I am currently in an Android project where our main view is a TabActivity and each tab is a separate Activity. One is a MapActivity and the other two are ordinary Activities.

First note that I think we must have each tab as separate activities, as there is too much code in the separate activities to just have the TabHost switch the content view on a tab change and have all of the code in the same class. Anyways, back to the problem.

One of the tabs include a button, which when pressed should make the TabActivity switch to the MapActivity and animate the map to a specific location.

The tutorial found on http://joshclemm.com/blog/?p=86 shows how to do it if the TabHost contains a mapview and a listview. If an item in the ListView is clicked, the TabHost switches to the mapview and animates to that location (those coordinates). This is exactly what i need to do when the button in the separate activity is pressed.

The MainView.java is created as follows:

public class MainView extends TabActivity implements OnTabChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

intent = new Intent().setClass(this, MapGUI.class);
    spec = tabHost.newTabSpec("map").setIndicator("Map",
                      res.getDrawable(R.drawable.ic_tab_menu_item))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MissionView.class);
    spec = tabHost.newTabSpec("mission").setIndicator("Mission",
                      res.getDrawable(R.drawable.ic_tab_menu_item))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SIPView.class);
    spec = tabHost.newTabSpec("call").setIndicator("Call",
                      res.getDrawable(R.drawable.ic_tab_menu_item))
                  .setContent(intent);
    tabHost.addTab(spec);

The MissionView.java is as follows:

public class MissionView extends Activity implements Observer{

MissionController mc;
private TextView missionheader, missiondescription, missionaddress, 
missiontime, missioninjuries;
private Button changedesc, gotoadress;
private String[] mission;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.missiontablayout);
    missionheader = (TextView)findViewById(R.id.missionheader2);
    missiondescription = (TextView)findViewById(R.id.missiondescription2);
    missionaddress = (TextView)findViewById(R.id.missionaddress2);
    missiontime = (TextView)findViewById(R.id.missiontime2);
    missioninjuries = (TextView)findViewById(R.id.missioninjuries2);

    changedesc = (Button)findViewById(R.id.gotoaddress);
    changedesc.setOnClickListener(new OnClickListener() {           
        public void onClick(View v) {
         // DO SOMETHING HERE?              
        }
    }); 
    mc = new MissionController(MissionView.this);
}

public void update(Observable observable, Object data) {
    if(data instanceof String[]){
        mission = (String[]) data;
        updateView(mission);
    }
}
public void updateView(String[] missiontext){
    missionheader.setText(missiontext[0]);
    missiondescription.setText(missiontext[1]);
    missionaddress.setText(missiontext[2]);
    missiontime.setText(missiontext[3]);
    missioninjuries.setText(missiontext[4]);
}

}

Anyone know how i could achieve this?

Note that the code supplied above has no implementation to actually draw to the actual location, but the question still remains, how do I make a button pressed in one activity to switch tab in the TabHost and fire a change on that tab activity?

Emil
  • 91
  • 1
  • 6

2 Answers2

0

Place the below line on button click where you want to switch to the Map activity

((MainView) getParent()).setTabMap();

and in MainView create the following function

public void setTabMap()
{
   //as Map activity is your first tab so pass 0 as index
   getTabHost().setCurrentTab(0);
}
iCoder
  • 1
  • 1
0

changing tabs in a TabHostcan easily be done with setCurrentTab(int) http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)

Sending events to other Activities can simply be achieved by sending a broadcast intent and receiving/handling it in the other Activity.

Alternatively you could save static references to all your tab Activities (ugly...) and call methods directly.

Goddchen
  • 4,459
  • 4
  • 33
  • 54