Following on from an earlier thread, I had wondered about creating linkedlists and have them be persistent across multiple activities. It was suggested that I extend the application class to do this so I decided to look into it.
I have since done this, assuming I haven’t made a mess of it, the app should now fill in a linkedlist with some example data that I had hoped to use in the main activity. But I can’t seem to access the data, and I’m a bit stumped.
Just getting started with android development so sorry if this is a ridiculous question. Anyway here is what i have so far.
The extending class GlobalData
package com.example.employeeList;
import java.util.LinkedList;
import android.app.Application;
import android.widget.Toast;
import com.example.employeeList.Employee;
public class GlobalData extends Application {
public LinkedList<Employee> EmployeeList;
@Override
public void onCreate(){
super.onCreate();
createEmployeeLinkedList();
}
public void createEmployeeLinkedList(){
// create a linked list
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Bob");
Employee emp3 = new Employee("Fred");
LinkedList<Employee> EmployeeList = new LinkedList<Employee>();
EmployeeList.add(emp1);
EmployeeList.add(emp2);
EmployeeList.add(emp3);
// The following code works, used for testing to see if linklist was actually being created.
// Employee boo = newEmployee();
// Employee boo = EmployeeList.getFirst();
// Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show();
}
}
Employee class
package com.example.employeeList;
import android.app.Application;
public class Employee extends Application{
public String name;
public Employee(String name) {
this.name = name;
}
}
MainActivity class
package com.example.employeeList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;
import com.example.employeeList.Employee;
public class MainActivity extends Activity {
protected GlobalData globalData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application instance
globalData = (GlobalData)getApplication();
//Would have thought I could just do this but apparently not.
Employee boo = globalData.EmployeeList.getFirst();
Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show();
}
}
The offending lines appear to be
Employee boo = globalData.EmployeeList.getFirst();
Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show();
Any ideas?
Try making GlobalData a singleton from Application class adding in your GlobalData class something like:
then you don’t need class Employee to extend Application too, its just a data model class.
Then you will have a instance of you GlobalData class (only one instance) in all the app live cycle, so you just can call in every activity/class/etc in you app something like:
or some stuff like that, as simple as that.
Notice that your GlobalData class will be instantiated when your app start and in the OnCreate method you will create your list and it will be “alive” all the app execution time. You can use the OnPause and OnResume methods from the Application class too in your class to interact with the app state changes.
Hope its help.