导航抽屉后退按钮堆栈错误

时间:2015-01-13 04:14:03

标签: android android-fragments transactions navigation-drawer back-stack

我有一个带导航抽屉菜单的应用, 问题是当我按下后退按钮时。

我在抽屉上有2个菜单=片段A,片段B 我去片段A1,然后去A2,然后我移动到片段B.然后我按下片段B上的后退按钮,屏幕返回到片段A ..我想如果我按片段B上的后退按钮应用程序很接近。 任何人都可以帮助我

这是我的代码

片段A1

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("ZZZ", "ada di oncreateView maintain");
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_layout_schedule_day, container, false);
        final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<6;i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("day", days[i]);
            aList.add(hm);
            // R.layout.listview_layout defines the layout of each item
            list = (ListView) rootView.findViewById(R.id.list);
            ListAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_schedule_day, new String[]{"day"}, new int[]{R.id.day});

            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    String day = ((TextView) view.findViewById(R.id.day)).getText().toString();
//                    Intent in = new Intent(getActivity(),FragmentScheduleDetail.class);
//                    in.putExtra("txt",day);

                    Fragment fragment = new FragmentScheduleRoom();
                    Bundle data = new Bundle();
                    data.putString("selectday", day);
                    fragment.setArguments(data);
                    FragmentManager frgManager = getFragmentManager();
                    Log.d("tag", "DAY SELECT: " + data.getString("selectday"));
                    FragmentTransaction tx = frgManager.beginTransaction();tx.replace(R.id.content_frame, fragment)
                    .addToBackStack( "schedule" ).commit();
                    //Toast.makeText(getActivity(), "You Clicked at " + aList.get(+position).get("day"), Toast.LENGTH_SHORT).show();
                }
            });

        }
        return rootView;
    }
}

片段A2

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d("ZZZ", "ada di oncreateView schedule");
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_layout_schedule_detail, container, false);
    new JSONParse().execute();
    return rootView;
}

片段B1

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_layout_bad, container, false);
        new JSONParse().execute();

        return rootView;
    }



    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            device_id = (TextView) getActivity().findViewById(R.id.device_id);
            name = (TextView) getActivity().findViewById(R.id.name);
            location = (TextView) getActivity().findViewById(R.id.location);
            status = (TextView) getActivity().findViewById(R.id.status);
            device_type= (TextView) getActivity().findViewById(R.id.device_type);

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Getting Bad Device List...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }



        @Override
        protected JSONObject doInBackground(String... string) {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.makeHttpRequestWithoutParams(url);
            Log.d("ZZZ", json.toString());
            return json;
        }


        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            oslist.clear();
            try {
                // Getting JSON Array from URL
                String statusR = json.getString(TAG_STATUS);
                if(statusR.equals("success")){
                    bad = json.getJSONArray(TAG_OS);
                    for(int i = 0; i < bad.length(); i++){
                        final JSONObject c = bad.getJSONObject(i);
                        // Storing  JSON item in a Variable
                        String device_id = c.getString(TAG_DEVICE_ID);
                        String name = c.getString(TAG_NAME);
                        String location = c.getString(TAG_LOCATION);
                        String status = c.getString(TAG_STATUS);
                        String device_type = c.getString(TAG_DEVICE_TYPE);
                        // Adding value HashMap key => value
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_DEVICE_ID, device_id);
                        map.put(TAG_NAME, name);
                        map.put(TAG_LOCATION, location);
                        map.put(TAG_STATUS, status);
                        map.put(TAG_DEVICE_TYPE, device_type);
                        oslist.add(map);
                        list=(ListView) getActivity().findViewById(R.id.list);
                        ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                                R.layout.list_bad,
                                new String[] {TAG_DEVICE_ID, TAG_NAME,TAG_LOCATION, TAG_STATUS, TAG_DEVICE_TYPE}, new int[] {
                                R.id.device_id, R.id.name, R.id.location, R.id.status, R.id.device_type});
                        list.setAdapter(adapter);
                        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view,
                                                    int position, long id) {
                                String device_id = oslist.get(position).get("device_id");
                                String device_type = oslist.get(position).get("device_type");
                                Log.d("device_id", "IDP clicked : " + device_id);
                                Log.d("device_type", "Type clicked : " + device_type);

                                if (device_type.length()<7)
                                {
                                    Fragment fragment = new FragmentScanServer();
                                    Bundle data = new Bundle();
                                    data.putString("device_id", device_id);
                                    fragment.setArguments(data);
                                    FragmentManager frgManager = getFragmentManager();
                                    Log.d("CLICK", "Send IDP to Maintenance Add : " + data.getString("device_id"));
//                                    frgManager.popBackStack("tag", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                    frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                                            .commit();
                                }
                                else
                                {
                                    Fragment fragment = new FragmentScanNetwork();
                                    Bundle data = new Bundle();
                                    data.putString("device_id", device_id);
                                    fragment.setArguments(data);
                                    FragmentManager frgManager = getFragmentManager();
                                    Log.d("CLICK", "Send IDP to Maintenance Add : " + data.getString("device_id"));
                                    frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                                            .commit();
                                }

2 个答案:

答案 0 :(得分:0)

使用getBackStackEntryAt从BackStack获取顶部片段并检查用户是否在F2片段上并按下后面然后关闭片段容器Activity。在Activity:

中覆盖onBackPressed()方法
  @Override
    public void onBackPressed() {
     FragmentManager fmanger = getSupportFragmentManager();
      if(fmanger.getBackStackEntryCount()>0){
      FragmentManager.BackStackEntry  backEntry=
                fmanger.getBackStackEntryAt(fmanger.getBackStackEntryCount()-1);
          String fragmentName=backEntry.getName();
          if(fragmentName.equals("schedule")){
                 // clear all fragment from Activity
                 // close Activity 
                 this.finish();
           }
     }
   }

答案 1 :(得分:0)

您必须对导航逻辑进行一些更改才能实现此目的。因此,每当您单击任何菜单项时,您需要清除堆栈中的所有片段并附加新片段。

清晰堆栈方法是

private void clearStack() {    
    FragmentManager frgManager = getFragmentManager();
    frgManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    frgManager.executePendingTransactions();
}

多数民众赞成。