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 8437353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:24:24+00:00 2026-06-10T07:24:24+00:00

I have a few Activities implement an Interface ( called NetworkListener). When requesting information

  • 0

I have a few Activities implement an Interface ( called NetworkListener). When requesting information from a remote server, they spawn an instance of a class (called NetworkRequest). NetworkRequest is a threaded class that sends requests to the server, waits for the response and sends it back to the requesting NetworkListener object. It could take up to 10 seconds to return data back to the NetworkListener instance. During that time, it is quite possible that that Activity or Fragment no longer exists, and a lot of the actions based on the server call
are no longer valid and will throw NPEs, such as:

((TextView) findViewById(R.id.server_status)).setText("Server response: " + response.getMsg());

What’s the best way to ensure that the requesting class is still alive and able to update UI?

Currently at the top of every method that receives calls back from the server I have:

if (getWindow() == null) {  // or getView()/getActivity() if Fragment
            finish();
            return;
        }

This seems inefficient and redundant. Is there a better method to what I’m trying to accomplish?

Thanks!

  • 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-10T07:24:25+00:00Added an answer on June 10, 2026 at 7:24 am

    You can move the logic checking state of activity / fragment to the NetworkRequest. Add isActive() method to NetworkListener interface.
    NetwrokRequest sends response to NetworkListener only if the latter is active.

    You can set activity / fragment activeness in onCreate() -> active and onDestroy() -> inactive.

    Just be wary of activity state changes like when rotating screen. Then, you get a new activity which doesn’t get server’s response, which might not be what you want.

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

Sidebar

Related Questions

I have an activity which is called by a few other activities. For example,
I have a set of activities starting from my home screen through a few
I have the following code in a few activities and it reads and parses
I have few basic questions on session management in GWTP. Client : GWTP, Server
I have a few dozen activities: 1-Main, 1-Animation, 10-Other's and it's sub-sets of more
I have few activities that make use of sqlite database, there is use of
In AndroidManifest.xml file I have registered a few activities that are described with the
I have few activities and a single service in my app. Most of these
My app has a few activities, all have an options menu, which is the
I have few questions about ssis transction isolation levels. consider a scenario:I have an

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.