Reputation: 3913
I am trying to implement a fragment layout. I have an activity that servers as a SplashScreen & fetches some data from the web and creates an ArrayList of my custom objects.
Normally If I were using a ListView I would just do the following.
private ArrayList<Articles> articles;
private void isComplete() {
Intent intent = new Intent(SplashScreen.this, ListActivity.class);
intent.putExtra("data", articles);
startActivity(intent);
finish();
}
How can I do pass the same data to a Fragment? Your help I much appreciated.
Upvotes: 3
Views: 7146
Reputation: 34765
use putExtra to pass value to an intent. use getSerializableExtra method to retrieve the data
If I have two activities A and B I want to pass ArrayList> value to Activity B then I will use the following code in class A
Pass ArrayList> data from Activity A to Activity B
Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("sunil", "sahoo");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivityForResult(intent, 500);
Retrieve the value in Activity B
In class B i will write the folowing code to retrieve the data
ArrayList<HashMap<String, String>> arl =(ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
Upvotes: -2
Reputation: 35956
Its simple take Global class
And declare public static ArrayList<Articles> articles = new ArrayList<Articles>;
In main Activity Global.articles = articles
;
Now u can use any where in Project.
Upvotes: 3
Reputation: 34765
It depends on the type of arraylist
putIntegerArrayListExtra(String name, ArrayList value)
putParcelableArrayListExtra(String name, ArrayList value)
putStringArrayListExtra(String name, ArrayList value)
putCharSequenceArrayListExtra(String name, ArrayList value)
Then you can read from you next activity by replacing put with get with key string as argument,eg
myIntent.getStringArrayListExtra("arrayPeople");
Upvotes: 2