因此,在我的MainActivity
中,我初始化了ArrayList
并将其传递给Fragment
到Bundle
。
然后我在ArrayList
中修改此Fragment
。然后,当我关闭/暂停应用时,我将ArrayList
保存在MainActivity
中 - 这反映了Fragment
中所做的更改,这让我感觉Bundle
正在通过引用的值,而不是原始数据的副本。
现在,假设我执行上述前两个步骤 - 初始化ArrayList
并将其传递给Fragment
到Bundle
,然后修改此ArrayList
in Fragment
。
当我暂停应用时(再次将我的ArrayList
保存在MainActivity
中),以便它在后台,有时候,仍然在Fragment
内,{{1}被摧毁了。当我再次打开应用程序时,已初始化已保存的ArrayList,但这一次,我没有将其传递给我的MainActivity
,因为还有一个可用的实例。
现在这就是我困惑的地方 - 重新打开Fragment
时,它包含所做的更改,即使这一次,我也没有通过将Fragment
加载到ArrayList
。
这是否意味着Fragment
传递了一个持续存在的引用值,即使Bundle
和Fragments
经历了其破坏阶段?
为了更清晰,以下是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();
}
}