pressing回不回previous片段 [英] Pressing back does not return to previous fragment

查看:148
本文介绍了pressing回不回previous片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与添加片段事务背面叠层的问题。我有一个主要活动中,我填充我的布局与菜单片段:

I have a problem with adding the fragment transactions to the back stack. I have a Main activity in which I populate my layout with a Menu Fragment:

public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

然后,MainMenuFragment内,用户选择一些选项,这导致与一些其他片段替换菜单片段

Then, inside the MainMenuFragment, the user chooses some option which results in replacing the menu fragment with some other fragment:

public class MainMenuFragment extends Fragment implements OnItemClickListener{
    GridView grid;
    FragmentManager manager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.main_menu_fragment, container, false);

    manager = getActivity().getFragmentManager();
    grid = (GridView) root.findViewById(R.id.gridView1);

    grid.setAdapter(new MenuTileAdapter(getActivity()));
    grid.setOnItemClickListener(this);

    return root;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    FragmentTransaction trans = manager.beginTransaction();
    if (position == 0){
        trans.replace(R.id.frag_container, new BasicSettingsFragment());
        trans.addToBackStack(null);
        trans.commit();
    }
}

}

有关我个人理解,这应该让这个当用户presses他们的设备上后退按钮,他们将被带回菜单中的片段,而是这个退出的应用程序。我究竟做错了什么?

For what i understand, this should make it so that when the user presses back button on their device, they will be brought back to the menu fragment, but instead this quits the app. What am i doing wrong?

推荐答案

您code是一个查询股价,你用ActionBarActivity从appcompat,而不是使用getSupportFragmentManager()和片段导入应该是appcompat一个,如果你决定用它。如果没有,用活动来代替ActionBarActivity和简单的片段导入与FragmentManager

Your code is a mixup, you use ActionBarActivity from appcompat and not using getSupportFragmentManager() and the fragments import should be the appcompat one if you decide to use it. If not, use Activity instead of ActionBarActivity and the simple Fragment import with FragmentManager

添加到您的活动安卓configChanges =keyboardHidden |方向|屏幕尺寸 这将阻止从当轮换重新启动您的活动。 使用 setRetainInstance(真)的片段。

Add this to your activity android:configChanges="keyboardHidden|orientation|screenSize" This will stop your activity from restarting when you rotate. use setRetainInstance(true) on fragments.

这篇关于pressing回不回previous片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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