Kolesar
Kolesar

Reputation: 1283

Relationship between Tabs - android

I want to create application with two Tabs, like follow:

MainClass:

private TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");

    firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(this,MainTab.class));
    secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,ResultTab.class));

    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
}

public TabHost getTabHost() {
    return tabHost;
}

FirstTab:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maintab);
    }

SecondTab:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.resulttab);
}

On First tab i have some buttons, with who i calculate some mathematic operations. A want to display result on SecondTab when calculating is finished.

First problem: I do not know, how can I send data (String) from FirstTab to SecondTab. I tray:

Second problem (small for now :) ): When I selected some parameters (my buttons, list, ...) on FirstTab and change view to SelectTab, and return to FirstTab selected parameters are gone.

Anyone help me?

Upvotes: 0

Views: 293

Answers (1)

Emir Kuljanin
Emir Kuljanin

Reputation: 3911

  1. You can send strings with intents. For example:

    Intent intent = new Intent().setClass(this, Secondtabclass.class); intent.putExtra("mykey", "string to send");

    startActivity(intent);

And then on the recieving activity you do:

String recievedString = this.getIntent().getStringExtra("mykey");
  1. You could save the state of your list and whatever you like if you override OnResume and OnPause methods. On pause gets called every time your activity loses focus, and on resume gets called every time your activity resumes focus. Like this:

    @Override protected void onResume() { super.onResume();

        //Do stuff
    }
    

Upvotes: 1

Related Questions