I would like to know what is the proper way to access non-primitive data between activities. Although there are many questions and answers on the topic, I still think that my question hasn't been asked yet.
1) The main activity shows a custom adapter ToDoListAdapter
. ToDoManagerActivity
loads data for entries in its methods loadItems()
and saveItems()
in onPause()
and onResume()
:
public class ToDoManagerActivity extends ListActivity {
toDoListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ToDoListAdapter(ToDoManagerActivity.this);
.....
}
}
2) Another activity TodoChartActivity
is fired from ToDoListAdapter
view and is an activity that shows all entries, but differently. In order to show the entries, I need the data that is in adapter variable in main activity.
According to sources in the internet, there are different ways to make adapter variable (or any other) accessible in the second activity:
- Make
mAdapter
public and static or make public getter&setter for it. BAD APPROACH - Access it from Application Singleton. BAD APPROACH
- Copy
loadItems()
andsaveItems()
fromToDoManagerActivity
and load them again into memory. Since I use memory much more that I actually need -- BAD APPROACH - Passing all data of one list entry through intent extras. TEDIOUS APPROACH, I was told. Also, let's imagine, that we need all entries, for example. So, it's a general question.
Do you have other suggestions, how to access complex objects and lists between activities?