Android的碎片,回去无需重新创建/重装片段 [英] Android Fragment, going back without recreating/reloading Fragment

查看:115
本文介绍了Android的碎片,回去无需重新创建/重装片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过不少问题上的SO有关片段,我仍然似乎无法弄清楚,如果是我想做的事情是可能的,更何况,如果我的设计模式就是有缺陷的,我需要重新工作的全过程。基本上,像已被问得最多的问题,我有NavigationTabs(使用ActionBarSherlock),然后每个选项卡中有一个FragementActivity,并且在选择行则FragmentActivities推新片段(我想一个动作条重新创建在iOS项目的Andr​​oid,它只是一些标签基本的基于导航的应用程序,可以深入了解具体信息)。当我点击previous片段被加载在手机上,但后面的按钮片段重新创建本身(所以Web服务是为每个视图再次调用),这是不需要的,因为信息不会在改变退步时previous视图。所以基本上我想弄清楚是怎么做的设置我的片段,这样,当我把手机上的后退按钮时,previous片段只是拉了已经创建了previous项目。下面是我目前的code:

I've seen quite a few questions on SO about Fragments and I still can't seem to figure out if what I want to do is possible, and more so if my design pattern is just flawed and I need to re-work the entire process. Basically, like most questions that have been asked, I have an ActionBar with NavigationTabs (using ActionBarSherlock), then within each Tab there is a FragementActivity and then the FragmentActivities push new Fragments when a row is selected (I'm trying to re-create an iOS Project in Android and it's just a basic Navigation based app with some tabs that can drill down into specific information). When I click the back button on the phone the previous Fragment is loaded but the Fragment re-creates itself (so the WebServices are called again for each view) and this isn't needed since the information won't change in a previous view when going backwards. So basically what I want to figure out is how do I setup my Fragments so that when I push the back button on the phone, the previous Fragment is just pulled up with the previous items already created. Below is my current code :

    //This is from my FragmentActivity Class that contains the ActionBar and Tab Selection Control
    @Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    int selectedTab = tab.getPosition();

    if (selectedTab == 0) {
        SalesMainScreen salesScreen = new SalesMainScreen();
        ft.replace(R.id.content, salesScreen);
    }
    else if (selectedTab == 1) {
        ClientMainScreen clientScreen = new ClientMainScreen();
        ft.replace(R.id.content, clientScreen);
    }.....

   //This is within the ClientMainScreen Fragment Class, which handles moving to the Detail Fragment
   row.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //Do something if Row is clicked
                            try{
                                String selectedClientName = clientObject.getString("ClientName");
                                String selectedClientID = clientObject.getString("ClientID");
                                String selectedValue = clientObject.getString("ClientValue");
                                transaction = getFragmentManager().beginTransaction();
                                ClientDetailScreen detailScreen = new ClientDetailScreen();
                                detailScreen.clientID = selectedClientID;
                                detailScreen.clientName = selectedClientName;
                                detailScreen.clientValue = selectedValue;
                                int currentID = ((ViewGroup)getView().getParent()).getId();
                                transaction.replace(currentID,detailScreen);
                                transaction.addToBackStack(null);
                                transaction.commit();

                            }
                            catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });....

     //And then this is the Client Detail Fragment, with the method being called to Call the Web Service and create thew (since what is displayed on this screen is dependent on what is found in the Web Service
          @Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
    return inflater.inflate(R.layout.clientdetailscreen, group, false);
}

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //Setup Preferences File Link
    this.preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

    //initialize the table object
    mainTable = (TableLayout)getActivity().findViewById(R.id.mainTable);

    //setup the detail table
    setupRelatedClientSection();

}

那么客户端详细屏幕可以向下钻取更多的时间,用同样的方法为客户端主界面​​,但是当我回去的新屏幕将细节画面的seuptRelatedClientSection()方法再次所以整个片段称为是重建的时候我真的只是想拉起该屏幕的已保存版本。这可能与我的当前设置,还是我处理这个错误的方式?

The Client Detail Screen can then drill down one more time, using the same method as the Client Main Screen but when I go back from that new screen to the Detail Screen the seuptRelatedClientSection() method is called again and so the entire Fragment is rebuilt when really I just want to pull up a saved version of that screen. Is this possible with my current setup, or did I approach this the wrong way?

推荐答案

我相信你正在寻找显示()隐藏()

I believe that you are looking for show() and hide().

我觉得你还是可以将其添加到backstack。

I think you can still add them to the backstack.

 transaction.hide(currentFragment);
 transaction.show(detailScreen);
 transaction.addToBackStack(null);
 transaction.commit();

我没有我的code来看待,但我相信这是它如何去......试试吧,除非别人有更好的办法。
我没有尝试过与展会()隐藏()backstack但我认为,它需要的是所做的更改之前的交易提交和将撤消他们,如果后退按钮是pssed $ P $。请回到我在这个事业我有兴趣知道的。

I didnt have my code to look at but i believe this is how it would go... Try it out unless someone else has a better way. I have not tried the backstack with show() hide() but i believe that it takes the changes that are made before the transactions commit and will undo them if the back button is pressed. Please get back to me on this cause i am interested to know.

您还必须确保您调用此之前的细节片段创建。由于它是基于someitem的点击,那么你或许应该创建的细节片段您单击以确保创建正确的细节片段每次。

You also have to make sure that the detail fragment is created before you call this. Since it is based on the click of someitem then you should probably create the details fragment every time you click to make sure the correct details fragment is created.

这篇关于Android的碎片,回去无需重新创建/重装片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆