简体   繁体   English

如何将StringBuilder数组从一个Activity传递到另一个Activity?

[英]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(这里可能是错误的)。


Here's how it's done: 以下是它的完成方式:

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;
}

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; 我个人认为这是处理这种情况的最合适的方法,你不想捆绑一个对象,但是还有另外两种不太理想的方法来维护Activity生命周期之间的对象状态 - 你可以将StringBuilder存储在Application类或Singleton对象中; 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. 如果将它存储在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. 此外,由于Application / Singleton具有全局范围,因此您必须确保将变量置零,以便它可以收集垃圾,或者它会在应用程序的整个生命周期内停留,只会浪费内存。 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. 但是,使用eventBus,变量仅在postStickyremoveStickyEvent命令之间“全局”存储,因此只要这些活动执行就会收集垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从StringBuilder获取价值并将其传递给另一个活动? - How to get value from StringBuilder and pass it to another activity? 如何通过列表<Integer>从一个活动到另一活动的数组 - How to pass List<Integer> array from one activity to another activity 如何将变量从一个活动传递到另一个活动? - How to pass variable from one activity to another? 如何将ArrayList中的类从一个活动传递给另一个活动 - How to pass Class of Class in ArrayList from one Activity to Another Activity 如何将数据从一个活动传递到另一活动 - How to pass data from one activity to another activity 如何将多个记录从一个活动传递到android中的另一个活动? - How to pass multiple records from one activity to another activity in android? 如何在Android中将两个字符串从一个活动传递到另一个活动 - How to pass two Strings from one Activity to another Activity in Android 如何将值从一个活动传递到另一个活动/适配器 - How to Pass a Value from One Activity to Another Activity/Adapter 如何将2D数组从一个活动传递到另一个活动(包括该数组包含的所有信息) - How to pass 2D array from one activity to another(including all the info the array contains) 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM