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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:51:43+00:00 2026-06-07T02:51:43+00:00

I am coding an app that give me some problems. I have an activity

  • 0

I am coding an app that give me some problems. I have an activity A with a button. When the user presses the button, it retrieves data from the Internet using an AsyncTask class, and when the data is retrieved, the AsyncTask class calls a method declared in activity A that starts a new activity B.

But this has a problem I don’t know how to fix. If the user presses the button twice (or more than once), the data is retrieved two times and activity B is launched twice. So, once I am in activity B, when I press the back button that instance of Activity B is destroyed but I’m still in the activity B (because it was launched twice).

Some code from the AsyncTask class which download the data:

@Override
protected String doInBackground(String... urls) {
    try {
        String data = downloadUrl(urls[0]);
        return downloadUrl(urls[0]);
    } catch (IOException e) {
        return m_errorCODE;
    }
}

@Override
protected void onPostExecute(String result) {
    ((MainActivity) mParentActivity).displayInfo(result);
}

Method displayInfo() from MainActivity which launches the activity B:

public void displayInfo(String result) {
        int duration = Toast.LENGTH_LONG;
        Toast toast = null;
        setSupportProgressBarIndeterminateVisibility(false);
        if (result == "404 ERROR") {
            toast = Toast.makeText(getBaseContext(), R.string.error_file_not_found, duration);
            toast.show();
        } else if (result == "CONNECTION_ERROR") {
            toast = Toast.makeText(getBaseContext(), R.string.error_connection, duration);
            toast.show();
        } else {

            Intent intent = new Intent(this, DisplayInfo.class);
            m_data = result;
            intent.putExtra(DOWNLOADED_DATA, m_data);
            intent.putExtra(FORMAT_OK, Boolean.toString(m_formatOK));
            intent.putExtra(URL_FILE, urlFile);
            startActivity(intent);
        }
    }

So I have three problems:

  1. How can I download the data only once?
  2. How can I open activity B only once?
  3. How can I know in my Activity A is still alive when it executes onPostExecute()?

To solve #2 I tried using intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) before starting activity B. It opens only one activity B, but if I press back, activity A starts again another instance of activity B.

Do I need to paste more code? Thanks in advance.

  • 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-07T02:51:45+00:00Added an answer on June 7, 2026 at 2:51 am

    For #1 and #2 you can use an instance variable in your activity that gets set after a button is clicked. In the onClick() method of the button you can check this variable to see if you should start the AsyncTask or if it was already started.

    For #3 you could maybe test if mParentActivity is not null? I am not sure about that though.

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

Sidebar

Related Questions

I have multiple app processes that each connect to servers and receive data from
I'm coding some ActionScript 3 app that needs to request some HTML page and
I'm currently coding an app that has hotkey functionality, I've done some reading and
I'm coding a small app that inserts data into an access database, so far
I'm coding up a REST/RPC API for a web app that I'm creating. From
I'm coding iOS app that will explain complex numbers to the user. Complex numbers
I'm coding an app that works much like Apple's Weather.app: There's a UIPageControl at
I'm coding a Windows Forms App that has a StatusStrip that displays status informations
I'm coding an app in Phonegap and I'm trying to extract the video-ID from
I am coding a small app ,in middle i struck at some point where

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.