Passing ArrayList between tabs

后端 未结 3 1518
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 02:25

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.

相关标签:
3条回答
  • 2021-01-07 03:02

    With the examples above, it was really work when I replace the "onStop" = "onPause"

    /** Called when the activity looses focus **/
    @Override public void onStop()
    {
        Intent myIntent = new Intent();
        myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
        this.setIntent(myIntent);
    }
    
    /** Called when the activity looses focus **/
    @Override public void onPause()
    {
        Intent myIntent = new Intent();
        myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
        this.setIntent(myIntent);
    }
    
    0 讨论(0)
  • 2021-01-07 03:09

    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.

    0 讨论(0)
  • 2021-01-07 03:15

    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?

    0 讨论(0)
提交回复
热议问题