如何在片段暂停时销毁片段

时间:2017-12-21 14:28:22

标签: java android fragment android-volley

我有一个片段通过volley连接获取数据,如果我打开这个片段并关闭它(按下后退按钮)然后再次打开它加载两次数据,如何在第一次暂停时销毁这个片段时间?我可以通过更简单的方法解决这个问题吗? 这是My Fragment Class的代码:

public class OurTeam extends Fragment{
    List<TeamMemberPojoClass> teamMembersList = new ArrayList<>();
    View myView;
    CardView mCardView;
    RecyclerView ourTeamRecyclerView;
    private Context mContext;
    TeamMembersAdapter adapter;
    ProgressBar progress;
    private String url = "http://tahrirlounge.net/event/api/getAllTeamMembers";//TODO : make class for urls
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView=inflater.inflate(R.layout.fragment_our_team,container,false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ourTeamRecyclerView = (RecyclerView) myView.findViewById(R.id.our_team_recycler_view);
        ourTeamRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        progress = (ProgressBar) myView.findViewById(R.id.progressBar_of_our_team);
        mCardView = (CardView) myView.findViewById(R.id.our_team_card_view);
        // Get the application context
        mContext = getActivity().getApplicationContext();
        // Initialize a new RequestQueue instance
        if(teamMembersList.size()==0){
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        if(teamMembersList!=null){
            teamMembersList.clear();
        }
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                Request.Method.GET,
                url,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            // Loop through the array elements
                            for (int i = 0; i < response.length(); i++) {
                                // Get current json object
                                JSONObject teamMemberObject = response.getJSONObject(i);
                                String memberName = teamMemberObject.getString("memberName");
                                String memberPosition = teamMemberObject.getString("memberPosition");
                                String memberDescription = teamMemberObject.getString("memberDecription");
                                String memberImage = teamMemberObject.getString("imagePath");
                                Log.v("Logging","member name is :"+memberName);
                                Log.v("Logging","member position is :"+memberPosition);
                                Log.v("Logging","member image is :"+memberImage);
                                TeamMemberPojoClass teamMember=new TeamMemberPojoClass();
                                teamMember.setName(memberName);
                                teamMember.setPosition(memberPosition);
                                teamMember.setDescription(memberDescription);
                                teamMember.setImage(memberImage);
                                teamMembersList.add(teamMember);
                            }
                            Log.v("Logging","teamMembers list is :"+teamMembersList);
                            Log.v("Logging","list size is : "+teamMembersList.size());
                            if (progress != null)
                                progress.setVisibility(View.GONE);
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                            if (progress != null)
                                progress.setVisibility(View.GONE);
                        }
                    }
                    //TODO : Caching , dimensions
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (progress != null)
                            progress.setVisibility(View.GONE);
                        Toast.makeText(mContext, "Check your connection and try again", Toast.LENGTH_SHORT).show();
                        FragmentManager fm = getActivity().getFragmentManager();
                        for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
                            fm.popBackStack();
                        }
                        getFragmentManager().beginTransaction().replace(R.id.content_frame,new Home()).commit();
                    }
                });
        requestQueue.add(jsonArrayRequest);}
        else{
        if (progress != null)
            progress.setVisibility(View.GONE);}
        adapter = new TeamMembersAdapter(getActivity(),teamMembersList);
        ourTeamRecyclerView.setAdapter(adapter);
        return myView;
}
}

0 个答案:

没有答案