Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:21:59+00:00 2026-06-18T14:21:59+00:00

Following on from an earlier thread, I had wondered about creating linkedlists and have

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:22:01+00:00Added an answer on June 18, 2026 at 2:22 pm

    Try making GlobalData a singleton from Application class adding in your GlobalData class something like:

        private static GlobalData singleton;
    
    public static GlobalData getInstance() {
        return singleton;
    }
    

    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:

    GlobalData.EmployeeList.get(0)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my earlier question about creating Address Books (many thanks Peter!), I
Following on from my earlier question, I have decided to have a go at
Following up from an earlier question I asked about, I'm trying to read data
Following on from this earlier question , I've had a look at the Heroku
I was reading an article earlier when I cam across the following quote from
Following on from an earlier question I'm trying to get multiple (widgets or in
Following up from an earlier question on extracting the n'th regex match , I
Following on from my earlier question , I am still having issues with loading
Following on from a post I made earlier, I am making progress with what
I have noted the following from a website: The JVM HotSpot memory is split

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.