How can I pass a StringBuilder array from one activity to another?

I've searched a lot of SO questions, they are old and can't find a better solution on, I don't want to convert each StringBuilder to a string to pass a string array , and how to achieve it?


source to share

4 answers

From within the source. Activity:

Intent intent = new Intent(this, DestActivity.class);
intent.putCharSequenceArrayListExtra("strings", myStringBuilders);


You may need to do something like new ArrayList(Arrays.asList(myStringBuilders));



Make it into a collection and then pass it using intent and then convert back to an array.



To pass an array of StringBuilder you can use putExtra Intent like:

Intent it = new Intent(this, YourActivity.class);
            StringBuilder[] sbAr = new StringBuilder[4];
            sbAr[0] = new StringBuilder("Array0");
            sbAr[1] = new StringBuilder("Array1");
            it.putExtra("sbParam", sbAr);


however I don't know exactly why, when you get the value for activity purposes, the StringBuilder array is restored as a CharSequence array.

CharSequence[] csA = (CharSequence[]) getIntent().getExtras().get("sbParam");


I hope this helps



The best way I've seen non-linking objects is using EventBus ( greenrobot / EventBus recommend ).

EventBus can be much faster than chaining. See this article .

The only downside I can see is that if your Activity / process gets destroyed due to low memory, it will lose data when it is recreated; Whereas, if you store data in a package, the package will be re-loaded into the Activity when it is recreated (maybe not the case here).

Here's how it's done:

Create a POJO to store the StringBuilder


public class SaveStringBuilderEvent {
    public StringBuilder sb;
    public SaveStringBuilderEvent(StringBuilder sb){ = sb; }


Create the first activity and publish the StickyEvent before starting the next activity ("StickyEvent" will keep the last event in memory until it is manually deleted):

public class Activity1 extends Activity {
    private StringBuilder sb;

    // Function for loading the next activity
    private void loadNextActivity(){
       EventBus.getDefault().postSticky(new SaveStringBuilderEvent(sb));
       Intent intent = new Intent(this, Activity2.class);



Create a second activity and remove the stick event from the EventBus:

public class Activity2 extends Activity {
    StringBuilder sb = EventBus.getDefault()



Personally, I think this is the most correct way to handle a situation like this where you don't want to bind an object, however there are two other less ideal ways to maintain the state of the object between the life cycles of an Activity - you can store the StringBuilder in a class Application

or in a Singleton object; however I would not recommend this because:

  • If you save it in the Application class, then you are the liter variables from all activities in the application.
  • Also, since Application / Singleton has a global scope, you must make sure it is null so that it can be garbage collected, or it will persist for the lifetime of the application, just wasting memory. However, using eventBus, the variable is only stored "globally" between commands postSticky

    and removeStickyEvent

    so it will get garbage collected whenever these actions are performed.


All Articles