2

I have read through the similar questions but do not see one like this. I have a simple calculator application there are two tabs. Each has their own activity class. I initially wrote this with a button on the first screen which onClick would take the inputs and pass them to the results screen which would do some calculation and then display the results. Now I want to do it with a TabHost. I have the two screens all set, but no idea how to take the inputs and pass them to the results activity to do the calculations and display the resulting values.

Thanks in advance Dean-O

Dean-O
  • 1,143
  • 4
  • 18
  • 35
  • I'd like to ask why you are using activities for each tab, rather than views? Generally speaking your code would perform better with views. – user432209 Dec 23 '10 at 23:42
  • how do you mean views? I know of activities for displaying content... how do you mean views? and how do you pass extras between views? – Dean-O Dec 24 '10 at 02:28

1 Answers1

4

The most natural way to do this would be to provide your own subclass of android.app.Application and use it to store the shared data. Then the first tab would set the values in the data structure, and the second tab would read them and use them to perform whatever calculation you wanted to do. See here: How to declare global variables in Android?

Assuming you don't want to take this approach and really want to use Intent extras to pass the data between Activities within a TabHost, you could do something like the following hack where you use the TabHosts Intent (accessed via getParent().getIntent()) to pass data back and forth.

 public class Tab1 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab_one);
  Button button = (Button) findViewById(R.id.btn);
  button.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    EditText x = (EditText) findViewById(R.id.x);
    EditText y = (EditText) findViewById(R.id.y);
    int a = Integer.parseInt(x.getText().toString());
    int b = Integer.parseInt(y.getText().toString());
    Intent i = getParent().getIntent();
    i.putExtra("a", a);
    i.putExtra("b", b);
    i.putExtra("tab", 1);
    TabActivity ta = (TabActivity) Tab1.this.getParent();
    ta.getTabHost().setCurrentTab(1);
   }
  });
 }
}


public class Tab2 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView result = new TextView(this);
  Intent i = getParent().getIntent();
  int a = i.getIntExtra("a", 0);
  int b = i.getIntExtra("b", 0);
  int sum = a + b;
  result.setText(Integer.toString(sum));
        setContentView(result);
 }
}
Community
  • 1
  • 1
michaelg
  • 2,692
  • 1
  • 17
  • 16
  • question, how are you setting the onClick to be when they click on the second tab? It appears you are using a button and not a Tab. You wrote Button button = (Button) findViewById(R.id.btn); button.setOnClickListener(new OnClickListener(){ – Dean-O Dec 24 '10 at 15:16
  • From your question I thought you wanted to initiate the transition from tab 0 to tab 1 using a button. If not, then you could put the state management code somewhere else. For example, you could put it in the Tab1's onPause method. Just move all of the onClick code there except the last two lines which manually transition between tabs. You could also move the code to the onTextChanged methods of the two EditText widgets. – michaelg Dec 27 '10 at 05:02