1

I have 2 activities. Each extends Activity. I tried all ways I found here, but none of them was working. I need to send String array from one to other activity, but stay in first activity. I try this:

Intent intent = new Intent(ActivityFrom.this, ActivityTo.class);
                                                intent.putExtra("string-array", ARRAY);
                                                ActivityFrom.this.startActivity(intent); 

And to recive:

 Intent intent = getIntent();
        String[] array = intent.getExtras().getStringArray("string-array");

Any idea?

user1254836
  • 1,999
  • 3
  • 13
  • 9

4 Answers4

0

I would suggest a Singleton class, this will also make your String array available throughout your application, if you so desire to use it later on. Something like this should get you started:

public class StringArrayHolder {
    private static StringArrayHolder instance;
    private String[] mArray;

    public static StringArrayHolder getInstance() {
        if (instance == null) {
            instance = new StringArrayHolder();
        }
        return instance;
    }

    private StringArrayHolder() {

    }

    public void setStringArray(String[] value) {
        mArray = value;
    }

    public String[] getStringArray() {
        return mArray;
    }

}

You could also use the putStringArrayListExtra() method.

Hope this helps.

Jean-Philippe Roy
  • 4,752
  • 3
  • 27
  • 41
0

You could use public Intent putExtra (String name, Bundle value) where your Bundle object contains the String[] array using putStringArray(String key, String[] value).

Trevor
  • 10,903
  • 5
  • 61
  • 84
0

This should do the trick for you

ActivityFrom.java:
...
    // Start ActivityTo
    Intent intent = new Intent(this, ActivityTo.class);
    intent.getExtras().putStringArray("string-array", ARRAY);
    startActivity(intent); 
...

ActivityTo.java:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] strings = getIntent().getStringArrayExtra("string-array");
    ...
}
Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
  • When activity stars by this: startActivity(intent); it covers whole screen and to continue i have to end it. Any idea here? – user1254836 Mar 07 '12 at 20:17
  • What do you mean by "continue"? The application activities are usually fullscreen on Android. If you have a question regarding creating an application that only covers parts of the screen, I suggest you create a separate question for this. – Sveinung Kval Bakken Mar 08 '12 at 07:50
0
Bundle b=new Bundle();
b.putStringArray("key", strarray);
Intent intent=new Intent(this, nextActivity.class);
intent.putExtras(b);
startActivity(intent);
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
  • When activity stars by this: startActivity(intent); it covers whole screen and to continue i have to end it. Any idea here? – user1254836 Mar 07 '12 at 20:18