How to pass an array list from one activity to another without starting it

前端 未结 4 1846
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 18:31

I have an activity which has an array list

ArrayList array = new ArrayList(); 

i want this array list to be pa

相关标签:
4条回答
  • 2021-01-06 18:45

    you can pass an intent to already running activity.. follow this http://www.helloandroid.com/tutorials/communicating-between-running-activities for that in the intent you can add an extra like this

    Intent contactsIntent = new Intent(getApplicationContext(),
                    ContactCards.class);
            contactsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetId);
    //Bundle containing the serialized list
            Bundle extraContacts = new Bundle();
    //Putting the array list templist is the array list here
    
            extraContacts.putSerializable("CONTACT_KEY", tempList);
            extraContacts.putString("CALL_STRING", CALL_STRING);
            contactsIntent.putExtras(extraContacts);
            contactsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(contactsIntent);
    
    0 讨论(0)
  • 2021-01-06 18:53

    You can declare you ArrayList as a static one like this,

    public static ArrayList<String> array = new ArrayList<String>(); 
    

    By doing this you can access your ArrayList from anywhere by

    activity_name.array;
    

    where activity_name is the activity or class in which you declare the static ArrayList

    0 讨论(0)
  • 2021-01-06 19:00

    use 1st activity

        Intent i=new Intent(ArraylistpassActivity.this,second.class);
       i.putStringArrayListExtra("key",arl);startActivity(i);
    

    2nd activity:

    arl=bundle.getStringArrayList("key");
    
    0 讨论(0)
  • 2021-01-06 19:02

    Based on the fact that you mention a 'Save' button, I think you would rather save this data to SharedPreferences or an SQLiteDatabase.

    I am unsure of what it would mean to 'save' some data to another Activity and not start it.

    With your data in a persisted state, you should be able to access it from any one of your other Activity's, which is what is sounds like you are after.

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