Passing ArrayList<String> between tabs
Posted
by Christophe
on Stack Overflow
See other posts from Stack Overflow
or by Christophe
Published on 2010-06-05T09:12:00Z
Indexed on
2010/06/05
9:22 UTC
Read the original article
Hit count: 276
Hi all,
I'm not very clear about the Intent object and how to use it to pass data between Activities. In my application I have several tabs between which I want to pass ArrayList. Here is a sample code I plan to use, but I'm missing the part where the main activity catches the Intent and passes it to the new activity on start :
1. myTabs.java ==> This is where I think I need to add some code to pass the data between TabOne and TabTwo. For now it is just using the sample code of the TabActivity sample.
public class myTabs extends TabActivity {
/** Called when the activity is first created. */
@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; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TabPeopleActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("TabOne").setIndicator("TabOne",
res.getDrawable(R.drawable.ic_tab_one))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, TabTransactionActivity.class);
spec = tabHost.newTabSpec("TabTwo").setIndicator("TabTwo",
res.getDrawable(R.drawable.ic_tab_two))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
2. TabOne.java ==> I added a piece of code in the onStop procedure to fill in the Intent data with the array I want to pass to TabTwo. Not sure it is the right way to do though.
public class TabOne extends Activity {
[...]
private ArrayList<String> arrayPeople;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabone);
arrayPeople = new ArrayList<String>();
[... here we modify arrayPeople ...]
}
/** Called when the activity looses focus **/
@Override
public void onStop(){
Intent myIntent = new Intent();
myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
this.setIntent(myIntent);
}
}
3. TabTwo.java ==> Here I am trying to fetch the ArrayList from the Intent that is supposed to be passed when the Activity starts. But how to do this?
public class TabTwo extends Activity {
private ArrayList<String> arrayPeople;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction);
Intent myIntent = new Intent();
myIntent = this.getIntent();
arrayPeople = myIntent.getStringArrayListExtra("arrayPeople");
}
}
Thanks for your ideas !
© Stack Overflow or respective owner