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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:22:46+00:00 2026-06-08T20:22:46+00:00

In my Android application, I have a fragment activity which it has a progressbar.

  • 0

In my Android application, I have a fragment activity which it has a progressbar. This progress bar set to visible when any fragment invoke the interface. Therefore, the code for fragment class is like this:

public class MainScreen extends FragmentActivity {

    public interface OnConnectingToInternet {
        public void showProgressbar(boolean flag);
    }

    // rest of codes
    .
    .
    .

    // Implementing Interface
    public void showProgressbar(boolean flag) {
        if(flag){
            myProgressbar.showProgressBar();
        } else {
            myProgressbar.hideProgressBar();
        }
    }

}

Each fragment should connect to Internet and get data, however before that they should invoke interface. I have written following code for one class (the code for other fragments is like this).

public class TopRecipesFragment extends Fragment {

    private OnConnectingToInternet onConnectingToInternet;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // onConnectingToInternet = (OnConnectingToInternet) activity;

        Log.i(TAG, "Fragment attached to activity.");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        New MyAsyncTask.execute();
    }

    public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {       

        @Override
            protected void onPreExecute() {
            Log.i(TAG, "myAsyncTask is about to start...");

            onConnectingToInternet.showProgressbar(true);
            }

        @Override
            protected Boolean doInBackground(Void... params) {
            ...
            }

        @Override
            protected void onPostExecute(Boolean result) {
            Log.i(TAG, "myAsyncTask is about to start...");

            onConnectingToInternet.showProgressbar(false);
            }

} 

The problem is I don’t know how to instantiate this interface. If I don’t put onConnectingToInternet = (OnConnectingToInternet) activity;, then, after running application, it will crash in a NullPointerException.

But, if I instantiate like that, then application crashes because of casting problem. I cannot instantiate it in normal way either, like onConnectingToInternet = new MainScreen();.

What is the solution? Any suggestion would be appreciated. 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-08T20:22:47+00:00Added an answer on June 8, 2026 at 8:22 pm

    I would write that You’re trying to do the following way:

    OnConnectingToInternet.java:

    public interface OnConnectingToInternet {
            public void showProgressbar(boolean flag);
        }
    

    MainScreen.java:

    public class MainScreen extends FragmentActivity implements OnConnectingToInternet {
    
    
    // rest of codes
    .
    .
    .
    
    // Implementing Interface
    @Override
    public void showProgressbar(boolean flag) {
        if(flag){
            myProgressbar.showProgressBar();
        } else {
            myProgressbar.hideProgressBar();
        }
    }
    }
    

    TopRecipesFragment.java:

    public class TopRecipesFragment extends Fragment {
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.i(TAG, "Fragment attached to activity.");
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            new MyAsyncTask((OnConnectingToInternet)getActivity()).execute();
        }
    
        // Make class static to avoid memory leaks
        public static class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
    
            // Keep WeakReference to the interface to avoid memory leaks
            private final WeakReference<OnConnectingToInternet> connectionRef;       
    
            MyAsyncTask(OnConnectingToInternet connection) {
                connectionRef = new WeakReference<OnConnectingToInternet>(connection);
            }
    
            @Override
            protected void onPreExecute() {
                Log.i(TAG, "myAsyncTask is about to start...");
    
                OnConnectingToInternet connection = connectionRef.get();
    
                if (null != connection) {
                   connection.showProgressbar(true);
                }
            }
    
            @Override
            protected Boolean doInBackground(Void... params) {
                ...
            }
    
            @Override
            protected void onPostExecute(Boolean result) {
                Log.i(TAG, "myAsyncTask is about to start...");
    
                OnConnectingToInternet connection = connectionRef.get();
    
                if (null != connection) {
                   connection.showProgressbar(false);
                }
           }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm building an android application which have a chat. in this chat i each
I have this configuration for my application. <application android:label=@string/app_name android:theme=@android:style/Theme.Holo.Light> <activity android:name=.MainActivity android:label=@string/app_name> I
When creating an Android application using Loaders, should every activity and fragment have its
In an Android application I have a fragment implemented that overrides onViewCreated to set
In my Android Application I have a Google MapView Activity and I am facing
I'm developing a simple Android application that would have an action bar on the
I would like to develop an Android application which will have a local database.
I have a SeachView inside of a Fragment in an Android 3.1 application currently
I am currently working on an Android tablet application which has two fragments, a
I have an Android application in the market which connects and send POST and

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.