Android - Bundle.getParcelableArraylist是否返回引用值的副本?

时间:2017-02-23 23:29:10

标签: java android android-bundle

因此,在我的MainActivity中,我初始化了ArrayList并将其传递给FragmentBundle

然后我在ArrayList中修改此Fragment。然后,当我关闭/暂停应用时,我将ArrayList保存在MainActivity中 - 这反映了Fragment中所做的更改,这让我感觉Bundle正在通过引用的值,而不是原始数据的副本。

现在,假设我执行上述前两个步骤 - 初始化ArrayList并将其传递给FragmentBundle,然后修改此ArrayList in Fragment

当我暂停应用时(再次将我的ArrayList保存在MainActivity中),以便它在后台,有时候,仍然在Fragment内,{{1}被摧毁了。当我再次打开应用程序时,已初始化已保存的ArrayList,但这一次,我没有将其传递给我的MainActivity,因为还有一个可用的实例。

现在这就是我困惑的地方 - 重新打开Fragment时,它包含所做的更改,即使这一次,我也没有通过将Fragment加载到ArrayList

这是否意味着Fragment传递了一个持续存在的引用值,即使BundleFragments经历了其破坏阶段?

为了更清晰,以下是Activity的{​​{1}}:

onCreate

这是MainActivity的{​​{1}}:

private ArrayList<Course> courses;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbarTitle = (TextView) toolbar.findViewById(R.id.textViewToolbarTitle);
    showHideToolbarListener = new ShowHideToolbarOnScrollingListener(toolbar);

    sharedPreferences = getSharedPreferences(getApplicationInfo().name, Context.MODE_PRIVATE);

    gson = new GsonBuilder().create();

    courses = loadData();

    if (savedInstanceState == null) {
        Fragment fragment = new MainFragment();
        Bundle arguments = new Bundle();
        arguments.putParcelableArrayList("courses", courses);
        fragment.setArguments(arguments);

        getFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, fragment)
                .commit();
    }


}

0 个答案:

没有答案