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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:39:08+00:00 2026-06-17T18:39:08+00:00

I have a fragment with UI that is responsible for doing short async api-calls.

  • 0

I have a fragment with UI that is responsible for doing short async api-calls. When an async task completes fragment triggers a Toast message with a result-string.

My issue is that if a user presses Back button without waiting for the result the app crushes due to a onPostExecute method, which in turn triggers Toast + getString(R.string.whatever) in the fragment that is already detached, because the activity is behind the onDestroy state.

I can check whether the fragment is detached or use a ProgressDialog, which is modal but I think it would be nice to show the toasts without blocking or any other quirks. Any suggestions on how to achieve it? Broadcast receiver in the application class == overkill?

Thanks.

UPD logs:

01-24 12:01:29.626: E/AndroidRuntime(616):
java.lang.IllegalStateException: Fragment
HandsetSocialNetworksFragment{40612a78} not attached to Activity 01-24
12:01:29.626: E/AndroidRuntime(616): at
android.support.v4.app.Fragment.getResources(Fragment.java:571) 01-24
12:01:29.626: E/AndroidRuntime(616): at
android.support.v4.app.Fragment.getString(Fragment.java:593)

  • 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-17T18:39:10+00:00Added an answer on June 17, 2026 at 6:39 pm

    At the start of the async call, get the activity from the fragment, and get the application context from it. Use that one later to get the string you need.

    Alternatively, you can fetch the string you need when you start the async call, instead of when you have to show the toast.

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

Sidebar

Related Questions

I have a fragment class that extends Fragment and calls setHasOptionsMenu to participate in
I have been using the new nested fragment API that Android includes in the
I have fragment that on a component click pop-ups DialogFragment. This dialog fragment holds
I have a Fragment that has contains lots of info (article) with a tabhost
I have a fragment that I need to display on the screen. I want
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
Using this sample : I have make my own Fragment that holds tabhost and
I have a BaseFragment so called LeaderboardFragment that extends Fragment : public class LeaderboardFragment
I have class that extend FragmentActivity in it I add fragment to layout as
I'm using Android-support-v4 I have a PagerAdapter that displays a list fragment in each

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.