后退按钮不会返回上一个片段

时间:2017-10-29 07:57:39

标签: android android-fragments

我有一个简单的Android应用程序,其中启动器活动是导航抽屉活动。当用户点击任何侧边栏按钮时,我会显示几个片段。活动显示" fragment1"首先。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        preferences = getSharedPreferences("Settings", 0);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        categories fragment = new categories();
        FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
        fragmenttransaction.replace(R.id.frame,fragment,"fragment1");
        fragmenttransaction.commit();

    }

问题是,当我移动到此活动中的其他片段并点按我的设备上的按钮,而不是回到之前的片段时,应用会立即关闭。

我尝试了一些方法,比如" popbackstack" inBackPressed函数内部活动,但不起作用

  @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);

        }

        if(getFragmentManager().getBackStackEntryCount() > 0){
            getFragmentManager().popBackStackImmediate();
        }

        else{
            super.onBackPressed();
        }

    }

片段类

public class settings extends Fragment {

    SharedPreferences preferences;
    private EditText rangevalue;
    private Button savebtn;

    private int range = 0;
 public settings() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_settings, container, false);

        preferences = getActivity().getSharedPreferences("Settings",0);
        rangevalue = (EditText)v.findViewById(R.id.rangeedittext);
        savebtn = (Button)v.findViewById(R.id.savebutton);

        rangevalue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                range = Integer.parseInt(rangevalue.getText().toString());
            }
        });


        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (range == 0)
                {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putInt("Range",2000);
                    editor.apply();
                }
                else
                {
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putInt("Range",range);
                    editor.apply();
                }
                Toast.makeText(getContext(), "Settings Saved", Toast.LENGTH_SHORT).show();
            }

        });

        return v;

    }

2 个答案:

答案 0 :(得分:0)

因为你没有将你的片段添加到backStack,如果你想要检索后者,可以使用这个mathod RandomTex[Index].a = RandomTex[Index].a + 1; 将你的片段保存在backStack中

.addToBackStack()

答案 1 :(得分:0)

以这种方式为我工作:

ProfileFragment profile_fragment = new ProfileFragment();

if(getFragmentManager() != null) {
     getFragmentManager()
            .beginTransaction()
            .add(R.id.frame_content, profile_fragment)
            .addToBackStack("back_profile_fragment")
            .commit();

然后让onBackPressed方法像这样:

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}