How to instantiate interface in fragment?

半城伤御伤魂 提交于 2019-12-06 05:26:20
sandrstar

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);
            }
       }
} 
Lucy

The problem is that sometimes a Fragment lives longer than its Activity, for example, if the Activity has a config change (e.g. rotated) then the Activity is destroyed, but the Fragment can be kept alive and then reattached to the new (rotated) Activity. See this post: Android Fragment lifecycle over orientation changes

So you might have a problem with the proposed solution using the WeakReference, because after a rotation you would have a reference to the old Activity (or maybe nothing).

Though I’m not sure why the line crashed in onAttach(), that should have been ok.

What seems to work for me:

1) When I need a ref to the Activity, call getActivity(). Do this right in onPostExecute().

2) Check the result for null (this can happen: Fragments can live longer than their Activities)

3) Check if activity isFinishing() – you don’t want to do certain UI things in that state.

4) Cast activity to your interface type.

5) Call showProgressBar().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!