我怎样才能从片段2回到片段1?如图所示

时间:2016-02-19 17:56:59

标签: android android-fragments android-navigation-drawer

片段1由导航抽屉打开,片段2由片段1打开现在我想从片段2返回到片段1,带有后退按钮,如图所示,并想从片段2中隐藏抽屉图标

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用点击或滑动事件

获取第二个片段的实例

SecondFragment fragment = new SecondFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();

答案 1 :(得分:0)

你可以通过添加Activity然后首先调用前一个片段中的活动,然后在活动中调用所需的片段,最后在活动上添加回按钮来实现。

public class DetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
       /* ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.header);
        actionBar.setDisplayHomeAsUpEnabled(true); */
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        setSupportActionBar(toolbar);
         getSupportActionBar().setTitle("Smart Take Away");
      //  getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (toolbar != null) {
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
     //   Bundle extras = getIntent().getExtras();

        if (savedInstanceState == null) {
            try {
                android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                DetailedListFragment fragment = new DetailedListFragment();
            //    fragment.setArguments(extras);
                fragmentTransaction.replace(R.id.detailfragment, fragment);
                fragmentTransaction.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}