1

我想问当我尝试回到另一个片段时如何检测。

所以我有这个代码:

fragmentManager.replace(R.id.fragment_searched_data_xml, fragment).addToBackStack(null).commit();

这让我回到那个片段。现在的问题是:

当我从当前片段按下后退按钮时,我可以回到初始片段,我可以检测到它吗?

public static class FragmentMainClass extends Fragment {
    int i;
    public FragmentMainClass() {
    }
    public FragmentMainClass(int i) {
        this.i = i;
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(
                R.layout.fragment_searched_data, container, false);
        TextView sn = (TextView) rootView.findViewById(R.id.stop_name);
        TextView sc = (TextView) rootView.findViewById(R.id.stop_code);

        String prefix = getArguments().getString("bussStopName");
        if (sn != null && sc != null) {
            if (prefix.contains("/")) {
                if (prefix.contains("/ ")) {
                    sn.setText(prefix.replace("\"", "").split("/ ")[0] + "\n" + prefix.replace("\"", "").split("/ ")[1]);
                    sc.setText(getArguments().getString("bussStopCode"));
                } else {
                    sn.setText(prefix.replace("\"", "").split("/")[0] + "\n" + prefix.replace("\"", "").split("/")[1]);
                    sc.setText(getArguments().getString("bussStopCode"));
                }
            } else {
                sn.setPadding(20, 40, 0, 0);
                sn.setText(prefix.replace("\"", ""));
                sc.setText(getArguments().getString("bussStopCode"));
            }
        }
        final LinearLayout child = (LinearLayout) rootView
                .findViewById(R.id.search_result_data_box);
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        //child.setVisibility(View.GONE);

        // params.height = 0;
        widthListView = container.getWidth();
        rootView.setTranslationX(-container.getWidth());
        rootView.animate().setStartDelay(300).translationX(0)
                .withEndAction(new Runnable() {

                    @Override
                    public void run() {
                        conreq = new ConnnectionRequest("97", getArguments().getString("bussStopCode"));
                        //expand(child);
                        initializeResultList(rootView);
                        // rootView.setVisibility(View.INVISIBLE);
                    }
                });
        return rootView;
    }

    public static void expand(final View v) {
        final int targtetHeight = v.getMeasuredHeight();

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT
                        : (int) (targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int) (targtetHeight / v.getContext().getResources()
                .getDisplayMetrics().density));
        v.startAnimation(a);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {

    }

    public void initializeResultList(View v) {
        result_list = (ListView) v.findViewById(R.id.list_view_results);
        //(PullToRefreshListView) v.findViewById(R.id.list_view_results);;
        final ResultDataCustomAdapter customAdapter = new ResultDataCustomAdapter(getActivity(), R.layout.pull_refresh_list_item, conreq.getCollectedData().getBusRouteSetData());

        //prlv.prepareForRefresh();
        ((PullToRefreshListView) result_list).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        result_list.setAdapter(customAdapter);
        result_list.setVisibility(View.VISIBLE);
        result_list.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> av, final View view,
                     final int i, long i2) {

                view.animate().setDuration(500).translationX(widthListView).alpha(0).
                withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        //setResult(Activity.RESULT_OK,new Intent().putExtra("bussStopCode", data.get(i).getStopCode()).putExtra("bussStopName", data.get(i).getStopName()));
                        ////int get 1
                        //data.remove(i);
                        int temporaryInteger = i;
                        //customAdapter.notifyDataSetChanged();
                        //view.setTranslationX(0);

                        Log.d("data",conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger - 1).getRouteHeading());
                        Bundle bundle = new Bundle();
                        bundle.putString("busdestination", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteHeading());
                        bundle.putString("busnumber", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteNo());
                        Fragment fragment = new FragmentNextTripForStop();
                        fragment.setArguments(bundle);
                        FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
                        fragmentManager.setCustomAnimations(R.anim.left_right_anim_x_left,R.anim.left_right_anim_x_right);
                        fragmentManager.replace(R.id.fragment_searched_data_xml, fragment).addToBackStack(null).commit();
                        result_list.setVisibility(View.GONE);
                       // finish();
                        //overridePendingTransition(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right);

                    }
                }); 
            }});

    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                ;
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] result) {

            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) result_list).onRefreshComplete();

            super.onPostExecute(result);
        }
    }

}
4

1 回答 1

0

在将您的片段添加到主机布局中时,将片段添加到堆栈中

 getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.home_fragment, fragment)
            .addToBackStack(fragment.getTag())
            .commit();

在你的 onBackPressed()

 if (getSupportFragmentManager().getBackStackEntryCount() >= 0) {
     getSupportFragmentManager().popBackStack();
 }else{
  super.onBackPressed();
 }
于 2019-07-23T12:55:01.093 回答