简体   繁体   English

Android Fragments不调用Backstack

[英]Android Fragments not calls a Backstack

I have a class "bancoActivity" that extends Fragment implements ActionBar.TabListener that calls another class "pagamentos" extends Fragment implements ActionBar.TabListener. 我有一个扩展了Fragment实现ActionBar.TabListener的类“ bancoActivity”,它调用了另一个“ pagamentos”扩展了Fragment实现ActionBar.TabListener的类。 When I'm in class "pagamentos" and click on the physical button "back" nothing happens, and when i click again the application finish. 当我在“ pagamentos”类中并单击物理按钮“ back”时,什么也没有发生,当我再次单击时,应用程序完成。 I leave there my code so that your can analyze. 我将代码保留在那里,以便您进行分析。 Obrigado. Obrigado。

part of the bancoActivity: bancoActivity的一部分:

@Override
public void onItemClick(AdapterView<?> customviewadapter, View view, int position, long id) {   
    listViewItem item = items.get(position);
    String Titulo = item.Title;

    if(Titulo.equals("Pagamentos")) {
        FragmentManager fragmentManager2 = getFragmentManager();
        FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
        pagamentos fragment2 = new pagamentos();
        fragmentTransaction2.hide(bancoActivity.this);
        fragmentTransaction2.add(android.R.id.content, fragment2);
        fragmentTransaction2.addToBackStack("banco");
        fragmentTransaction2.commit();
    }
}

part of the pagamentos: 帕加门多斯的一部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     getActivity().setContentView(R.layout.pagamentos);
     FragmentManager fm = getFragmentManager();

the two activities extends and implements: 这两个活动扩展并实现:

public class pagamentos extends Fragment implements ActionBar.TabListener{
public class bancoActivity  extends Fragment implements ActionBar.TabListener

I'm a newbie too, but I'll take a stab at it... 我也是新手,但我会刺中它...

I think you need to instantiate both fragments from the activity that holds them, and use 1 fragment manager to do the switching. 我认为您需要从保存它们的活动中实例化这两个片段,并使用1个片段管理器进行切换。 Its hard for me to tell without your whole code, but I have a similar setup working, here is how: 没有完整的代码,我很难分辨,但是我有一个类似的设置正在工作,方法如下:

public class MainActivity extends Activity implements ActionBar.TabListener {

    public static final int SEARCH_FRAG = 1;
public static final int MAP_FRAG = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragMan = (FragmentManager) getFragmentManager();
    mapFrag = ExtendedMapFragment.newInstance();
    searchFrag = SearchFragment.newInstance();
    if (mapShown == false) {
        swapFrags(SEARCH_FRAG);
    } else {
        swapFrags(MAP_FRAG);
    }
     ActionBar actionBar = getActionBar();
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     actionBar.setDisplayShowTitleEnabled(true);
     searchTab = actionBar.newTab()
                .setText(R.string.search_tab_label)
                .setTabListener(this);
        actionBar.addTab(searchTab);
     mapTab = actionBar.newTab()
             .setText(R.string.map_tab_label)
             .setTabListener(this);
        actionBar.addTab(mapTab);
     if (savedInstanceState != null) {
         actionBar.setSelectedNavigationItem(savedInstanceState.getInt("currentTab"));
     }
     actionBar = null;

}

    public void swapFrags(int whatFrag) {
    if (whatFrag == SEARCH_FRAG) {
        //switch to frag 1, ie searchFrag
        FragmentTransaction trans = fragMan.beginTransaction();
        trans.replace(R.id.map, searchFrag);
        mapShown = false;
        trans.addToBackStack(null);
        trans.commit();
    }
    if (whatFrag == MAP_FRAG) {
        //switch to frag 2, ie mapFrag
        if (lbServ != null) {
            update.autoCenter(lbServ.getCurrentLatLng());
        }
        FragmentTransaction trans = fragMan.beginTransaction();
        trans.replace(R.id.map, mapFrag);
        mapShown = true;
        trans.addToBackStack(null);
        trans.commit();
    }
}

}

The swapFrags() method is also called from my onTabSelected callback. 还从我的onTabSelected回调中调用了swapFrags()方法。 I think, since I only have one fragment manager, and the same manager is calling all the addToBackStack() methods, it is more organized. 我认为,由于我只有一个片段管理器,并且同一管理器正在调用所有的addToBackStack()方法,因此它的组织性更高。 When I open the app, select a new tab, then hit physical back key, it goes back to the previous tab, which is what you are after, no? 当我打开应用程序时,选择一个新选项卡,然后按物理后退键,它将返回到上一个选项卡,这是您要执行的操作,不是吗?

One thing I found difficult to learn with fragments is that all calls, keys, etc. go to the activity first. 我发现很难用片段学习的一件事是所有呼叫,键等都首先进入活动。 Push a button on the fragment, the activity that holds it gets the callback first (whether it uses it or not), then if there is a listener in the fragment it will get the call also, but fragments can't do really anything outside themselves, and fragment transactions involve objects outside the fragment. 按下片段上的一个按钮,保存它的活动将首先获得回调(无论是否使用它),然后,如果片段中有一个侦听器,它也会获得调用,但是片段实际上不能在外部做任何事情本身,碎片事务涉及碎片外部的对象。

I suppose you could set up an interface between fragment and activity, with a method like swapFrags() in the activity, where the fragment can ask to be swapped, that should do it too I think. 我想您可以在片段和活动之间建立一个接口,在活动中使用诸如swapFrags()之类的方法,片段可以要求被交换,我认为也应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM