[英]How to pass StringBuilder Array from one Activity to another?
我搜索了很多SO问题,它们已经老了,在docs.oracle.com上找不到更好的解决方案,我不想将每个StringBuilder转换成字符串来传递一个字符串数组,那么如何实现呢?
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));
您可能需要执行类似new ArrayList(Arrays.asList(myStringBuilders));
将其转换为集合,然后将其传递给intent,然后将其转换回Array。
To pass an StringBuilder array you can use the putExtra of Intent, like this: 要传递一个StringBuilder数组,你可以使用Intent的putExtra,如下所示:
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. 但是我不知道为什么当你将检索活动目标中的值时,StringBuilder数组被恢复为CharSequence数组。
CharSequence[] csA = (CharSequence[]) getIntent().getExtras().get("sbParam");
I hope this helps 我希望这有帮助
The best way I've seen to pass around objects without bundling is to use an EventBus (I recommend greenrobot/EventBus ). 我看到在没有捆绑的情况下传递对象的最好方法是使用EventBus(我推荐greenrobot / EventBus )。
An EventBus can be much faster than bundling. EventBus可以比捆绑快得多。 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; 我能看到的唯一不利的一面是,如果你的Activity /进程由于内存不足而被破坏,它将在重新创建时丢失数据; Whereas, if you store data in a bundle, the bundle will be resupplied to the Activity when it is recreated (could be wrong here). 然而,如果您将数据存储在一个包中,那么该包将在重新创建时重新提供给Activity(这里可能是错误的)。
Create a POJO to store the StringBuilder
for the event: 创建一个POJO来存储事件的StringBuilder
:
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): 创建第一个Activity,并在开始下一个Activity之前发布StickyEvent('StickyEvent'会将最后一个事件保留在内存中,直到手动删除):
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: 创建第二个活动并从EventBus中删除stick事件:
public class Activity2 extends Activity {
StringBuilder sb = EventBus.getDefault()
.removeStickyEvent(SaveStringBuilderEvent.class)
.sb;
}
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; 我个人认为这是处理这种情况的最合适的方法,你不想捆绑一个对象,但是还有另外两种不太理想的方法来维护Activity生命周期之间的对象状态 - 你可以将StringBuilder存储在Application
类或Singleton对象中; however, I wouldn't recommend this because: 但是,我不建议这样做,因为:
postSticky
and removeStickyEvent
commands, so it will get garbage collected whenever those Activities do. 但是,使用eventBus,变量仅在postSticky
和removeStickyEvent
命令之间“全局”存储,因此只要这些活动执行就会收集垃圾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.