[英]How can I go back from fragment 2 to fragment 1 ? as shown in the picture
You can use a click or swipe event 您可以使用点击或滑动事件
get the instance of your second fragment 获取第二个片段的实例
SecondFragment fragment = new SecondFragment();
fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();
You can do this by adding Activity and then first call activity from previous fragment then call your desired fragment in activity and finally add back button on the 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.