How to pass a String array value from one activity to another Activity in android?

前端 未结 6 1542
北荒
北荒 2020-12-15 14:20

I am having two activities in my application. I want to pass tha array of String from one activity to another.. How to pass this values from activity to activity?

相关标签:
6条回答
  • 2020-12-15 14:20

    You can consider using Intent.getStringArrayExtra

    In the first activity:

    Intent intent = new Intent(context, NewActivity.class);
    intent.putExtra("string-array", stringArray);
    context.startActivity(intent);
    

    and in the second one:

    Intent intent = getIntent();
    String [] stringArray = intent.getStringArrayExtra("string-array");
    
    0 讨论(0)
  • 2020-12-15 14:26

    In Activity One, write this code to create an array and pass it to another activity:

    String[] array1={"asd","fgh","dcf","dg","ere","dsf"};
    Intent i=new Intent(MainActivity.this,Main2Activity.class);
    i.putExtra("key",array1);
    startActivity(i);
    

    In Second Activity, write this to retrieve your array

    String[] array = getIntent().getStringArrayExtra("key"); 
    
    0 讨论(0)
  • 2020-12-15 14:28

    Intent myIntent = new Intent(getApplicationcontext, NewActivity.class); intent.putExtra("mStringArray", mystringArray); startActivity(myIntent);

    in the New activity in onCreate event

    String[] mystringArray = getIntent().getStringArrayExtra("mStringArray");

    if you want to send more data with different data types you shoud use BUNDLE.

    0 讨论(0)
  • 2020-12-15 14:35

    just serialize it and set it in the extras of the intent (of activity) you wanna open.
    You will receive it in the onCreate() of that activity.
    Convert it to array again.

    0 讨论(0)
  • 2020-12-15 14:35

    Its very simple, make that variable static & make one public static method in that class like

    public static getArray() 
    { 
            return array; 
    } 
    

    Now access this method from another activity, where you want to access it.

    0 讨论(0)
  • 2020-12-15 14:47

    Here's some reading: http://www.vogella.de/articles/AndroidIntent/article.html#overview_accessdata go to section 2.1.

    Also, How to pass ArrayList using putStringArrayListExtra() should explain something similar.

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