按后退按钮当前片段持久不恢复以前的片段

时间:2013-09-05 08:12:07

标签: java android-fragments android-event

我有片段活动,其中包含用于片段转换的Framelayout。名为LeadActiviy的片段在SherlockListFragment的方法Asynctask内容AsyncTask块语句中扩展onPostExcute包含else。在else块中,由ItemNotFound替换的当前片段扩展了Fragment。按下后退按钮后,在framelayout中设置ItemNotFound的内容后,它的当前状态仍然存在。没有出现它以前的片段。请帮我解决问题 -

来自LeadActivity的AsyncTask方法onPostExecute ...

  protected void onPostExecute(String file_url){
    pDialog.dismiss();
    //FragmentManager fm=getFragmentManager();
    //final FragmentTransaction ft=fm.beginTransaction();
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if(success==1){
            // TODO Auto-generated method stub
            ListAdapter adapter = new SimpleAdapter(
                    getActivity(), leadlist,R.layout.leadlistview, new String[] { Tag_Lead},
                    new int[] { R.id.textView6});
            // updating listview
            setListAdapter(adapter);
            //get leads of order
            ListView lv=getListView();
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    String pid=((TextView) arg1.findViewById(R.id.textView6)).getText().toString();

                    LeadDetail frag=new LeadDetail();
                    Bundle bnd=new Bundle();
                    bnd.putString("lead",pid);
                    frag.setArguments(bnd);
                    FragmentManager fm=getFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
                    ft.replace(R.id.content_frame, frag);
                    ft.addToBackStack(null);
                    ft.commit();
                }

            });
            }
            else {
                String e="Lead Not Found";              
                ItemNotFound frag=new ItemNotFound();
                //Frag1 frag=new Frag1();
                Bundle bnd=new Bundle();
                bnd.putString("data",e);
                frag.setArguments(bnd);
                FragmentManager fm1=getFragmentManager();
                FragmentTransaction ft1=fm1.beginTransaction();
                ft1.replace(R.id.content_frame, frag,st);
                ft1.addToBackStack(st);
                ft1.commit();
               }
        }

ItemNotFound的代码.............

  public class ItemNotFound extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    View V = inflater.inflate(R.layout.itemnotfound, container, false);
    TextView tv=(TextView)V.findViewById(R.id.textView7);
    Bundle bnd=this.getArguments();
    String s=bnd.getString("data");
    tv.setText(s);
    return V;
    }

   }

1 个答案:

答案 0 :(得分:0)

您可以看到我的解决方案here

我有一个ViewPager,其中有三个页面包含Fragments。在第一页中,我有ListFragmentonListItemClick我将Fragment替换为另一个Back Button。按下ListFragment后,我会返回{{1}}。