3

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it worked but static variables take space in memory!! any other way possible??
@Nischal yes you can for ref stackoverflow.com/questions/14997855/…
2

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");

Comments

-2

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);

1 Comment

Question is about ListFragment. Not about Activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.