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

  • Home
  • SEARCH
  • 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 6975181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:20:07+00:00 2026-05-27T17:20:07+00:00

Situation: When quitting a main activity of an android app, I wish to call

  • 0

Situation:
When quitting a main activity of an android app, I wish to call a method on a living instances of an Activity to make them clear data in arrays (so that the arrays are not already populated when returning to the app).

In MainActivity.java

// ...
@Override
public void onDestroy() {       
   super.onDestroy();
   // call method to clear an array list of its data
   // NullPointerException here if OtherActivity has no current living instance
   OtherActivity.clearAllData();        
}

In OtherActivity.java

// ...
private static ArrayList <String> myStringCollection;
// ...
public static void clearAllData(){
   if(myStringCollection.size() > 0){
     myStringCollection.clear();
     Log.d(TAG, "clearing data in OtherActivity");
   }

}

Question:
How can we know whether OtherActivity has a current living instance so that we can make a safe call to OtherActivity.clearAllData() ?

What the code attempts to do:
After quitting the app and returning to it, some ArrayLists still have old data in them, and as a result the new data (duplicate) gets stacked on top. This code attempts to clear data from the ArrayLists on the onDestroy() of the main activity, so that there will be no old data when returning to the app later.

  • 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-05-27T17:20:08+00:00Added an answer on May 27, 2026 at 5:20 pm

    Your code is currently using static variables. Static variables are attached to the ClassLoader not the actual instance of the class. This means you don’t actually need a valid living instance to do what you are doing above. Just use a check like myStringCollection != null to guarantee that your variable has been initialized and you should be fine.

    Note, please clarify if you really do want to access non-static variables.

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

Sidebar

Related Questions

Situation Well, as you can see below, I have a main App, creating a
Situation: I want to provide a website service where users can enter some data
Situation in short. For some reason reverse() method does not work. in PROJECT urls.py
Situation I'd like to make an RPC interface easier to use. This is a
Situation: It seems impossible to store any objects in the CI_Session since, reading them
Situation: typedef int TypeA; typedef int TypeB; I need to make TypeA incompatible with
Situation: The UI of an app is based on a UISplitView with two UINavigationController
Situation: I am trying to construct a simple method that accepts two different integers
Situation: I want to track some Data with Google Analytics on the server-side. Problem:
Situation: I have two dojo autocompleters on a jsp. Both of them trigger the

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.