4

我有一个带有两个片段的活动。这里我以编程方式在第二个片段中提供一个后退按钮以返回到第一个片段。

这是我的片段1代码:

 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
      V = inflater.inflate(R.layout.lm_fragement, container, false);
     listview = (ListView)V.findViewById(R.id.listView1);
     url="http://victorysites.org/apps/SSPC/homeinfo.php?UDID=12341234";

     JsonParsing json=new JsonParsing();
     list=json.getData(url);
     System.out.println("length :"+list.size());

     listview.setAdapter(new MyAdapter(getActivity(),list));


     final ProgressDialog waitdialog;



      listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Bundle data = new Bundle();
            data.putString("name",""+arg2);
            Fragment f;
            f = new PM_Fragment();
            f.setArguments(data);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(android.R.id.content, f);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit(); 
        }
    });

    Button bt=(Button)V.findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bundle data = new Bundle();
            data.putString("name","Venkat");
            Fragment f;
            f = new PM_Fragment();
            f.setArguments(data);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(android.R.id.content, f);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit();
        }
    });


    return V;
}

}

这是我的fragment2代码:

  public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    f=new PM_Fragment();
      V = inflater.inflate(R.layout.pm_fragment, container, false);
     Bundle bundle = this.getArguments();
      String myInt = bundle.getString("name");
      System.out.println("NAME"+myInt);
      TextView tv=(TextView)V.findViewById(R.id.portrait_message);
      tv.setText(myInt);

    Button bt=(Button)V.findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /*Fragment f=new PM_Fragment();
            getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();*/
            //getActivity().getFragmentManager().beginTransaction().remove(this).commit();
        getFragmentManager().popBackStack();


        }
    });


    return V;
}

}

它工作正常,但问题是它重新加载上一个(第一个片段)屏幕。但我不想重新加载页面,我希望它像模拟器中的默认后退按钮一样工作。

任何人都可以给我一个想法。提前致谢。

4

1 回答 1

1

在您的应用程序中使用片段时,单个FragmentTransaction对象可能表示应添加到后台堆栈的上下文更改。如果您通过交换片段在手机上实现master/detail流程,您应该确保Back button在详细屏幕上按 会使用户返回到主屏幕。为此,请在提交事务之前调用 addToBackStack():

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

*当回栈中有FragmentTransaction对象并且用户按下 时Back button,将FragmentManager最近的事务从回栈中弹出并执行相反的操作(例如,如果事务添加了片段,则删除它)。*

注意You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when adjusting filters). For more information, about when Back navigation is appropriate, see the Navigation design guide.

添加

如果您的应用程序更新其他用户界面元素以反映片段的当前状态,例如操作栏,请记住在提交事务时更新 UI。除了提交事务之外,您还应该在返回堆栈更改后更新您的用户界面。您可以通过设置 FragmentManager.OnBackStackChangedListener 来监听 FragmentTransaction 何时恢复:

getSupportFragmentManager().addOnBackStackChangedListener(
        new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                // Update your UI here.
            }
        });

请参阅此链接了解更多详情

于 2013-09-22T07:25:27.637 回答