views:

398

answers:

2

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 !

EDIT :

Okay i'll make it simple, here is the complete workspace of the project :

http://www.lecompteestbon.com/Android.LCEB.zip

What i want to do is an offline version of the website lecompteestbon, which allows people to do accounting between friends after a weekend.

TabPeople = Add the list of friends
TabTransactions = List of expenses
TabTransaction = Add an expense
TabResult = Calculate the list of payments

Let me know how to make this work :)

A: 

I don't think altering the intent in onStop() will work because the TabHost handles the intents. Setting one class' intent with this.setIntent() wont let another class access it with getIntent() as they are built on different intents.

I would either store my arrayPeople in a database or I would pass arrayPeople back to the TabHost with MyTabs.setArrayPeople or something similar. Then you can query the db onCreate of your next tab or just pull it from your MyTabs Class.

disretrospect
Using a database seems too advanced for this simple thing. When you mean using "MyTabs.setArrayPeople" is it a similar solution as proposed above?
Christophe
Yes using a static ArrayList would be best.
disretrospect
A: 

What's about setting a static field into the myTabs object?

    public class myTabs extends TabActivity {
....
public static arrayPeople = new ArrayList<String>();
....

On TabOne.java:

 @Override
    public void onStop(){
       myTabs.arrayPeople = arrayPeople;
    }

On TabTwo.java:

arrayPeople =  myTabs.arrayPeople

Does make sense?

Francesco
It looks simple and interesting, but it gives me a "(Suspended (exception RuntimeException))" when I try to look into the content. I get only [PRINT] : System.out.println("arrayPeople [PRINT]"); System.out.println("arrayPeople 1=[" + arrayPeople.get(0) + "]"); System.out.println("arrayPeople [PRINT OK]");Anything I am doing wrong?
Christophe
You should post your code. Edit your question and add the new piece of code :)
Francesco
Okay, see the details I have added with the complete workspace for download
Christophe