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 ?
-
Is making them static a viable option?Roberto Anić Banić– Roberto Anić Banić2015-07-24 01:31:05 +00:00Commented Jul 24, 2015 at 1:31
-
not at all , static are visible and editable from other classes , i don't want to allow that.Zulqurnain Haider– Zulqurnain Haider2015-07-24 01:32:21 +00:00Commented 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!Roberto Anić Banić– Roberto Anić Banić2015-07-24 01:35:20 +00:00Commented 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.Zulqurnain Haider– Zulqurnain Haider2015-07-24 01:38:16 +00:00Commented 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.bcorso– bcorso2015-07-24 02:48:21 +00:00Commented Jul 24, 2015 at 2:48
4 Answers
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));
3 Comments
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
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:
- If you store it in Application class then you liter variables from all Activities into the Application.
- 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
postStickyandremoveStickyEventcommands, so it will get garbage collected whenever those Activities do.