3

I've searched on many SO questions , they are to old and can't find better solution on docs.oracle.com , i don't want to convert Each StringBuilder to string to pass an string array so how to achieve it ?

5
  • Is making them static a viable option? Commented Jul 24, 2015 at 1:31
  • not at all , static are visible and editable from other classes , i don't want to allow that. Commented Jul 24, 2015 at 1:32
  • By the way, you do know that if you set them to static only the guy who coded the app can really make other classes access that variable. Other than that there is no other way of doing what you want without serializing or parcelizing the object! Commented Jul 24, 2015 at 1:35
  • According to java docs StringBuilder implements Serializable , so i think it is possible , yet i tried that serializable object passing that didn't work out. Commented Jul 24, 2015 at 1:38
  • @BadComputer I would highly recommend using an EventBus for this. Check out my answer for how to do this. Commented Jul 24, 2015 at 2:48

4 Answers 4

2

From inside the source Activity:

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

You might have to do something like new ArrayList(Arrays.asList(myStringBuilders));

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

3 Comments

basically the stringBuilder contains very huge data , converting it is not a viable option , if i convert i have to again convert to StringBuilder at receiving side, so that option is out of the table.
If it is really that large then you should just make a package local static class variable (preferably not on the Activity)
can you complete your above answer , on receiving side , i got to try that , and check . making that static is for last resort but i didn't think of passing an advanced string class like StringBuilder would be that complex ?
0

Make it into a Collection and then pass it through an intent, and then convert it back to an Array.

Comments

0

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

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 do not know exactly why when you will retrieve the value in activity targets the StringBuilder array is recovered as CharSequence array.

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

I hope this helps

Comments

0

The best way I've seen to pass around objects without bundling is to use an EventBus (I recommend greenrobot/EventBus).

An EventBus can be much faster than bundling. See this article.

The only down side I can see is that if your Activity/process is destroyed because of low memory it will lose the data when it's recreated; Whereas, if you store data in a bundle, the bundle will be resupplied to the Activity when it is recreated (could be wrong here).


Here's how it's done:

Create a POJO to store the StringBuilder for the event:

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

Create first Activity, and post a StickyEvent before starting the next Activity (a 'StickyEvent' will keep the last event in memory until it is manually removed):

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);
       startActivity(intent);
    }

    ...
}

Create Second Activity and remove the stick event from the EventBus:

public class Activity2 extends Activity {
    StringBuilder sb = EventBus.getDefault()
                           .removeStickyEvent(SaveStringBuilderEvent.class)
                           .sb;
}

NOTE:

Personally I think it's the most proper way to handle such a situation where you don't want to bundle an object, however there are two other, less ideal, ways to maintain the state of an object between Activity life-cycles -- you could store the StringBuilder in the Application class or in a Singleton object; however, I wouldn't recommend this because:

  1. If you store it in Application class then you liter variables from all Activities into the Application.
  2. In addition, since the Application/Singleton have global scope, you have to make sure to null the variable so it can get garbage collected or it will stick around for the entire lifetime of the application, just wasting memory. However, using the eventBus, the variable is only stored "globally" between the postSticky and removeStickyEvent commands, so it will get garbage collected whenever those Activities do.

Comments

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.