在我的应用程序中,我有一个导航抽屉,它基本上是主要活动,在此活动中,我还实现了Firebase UI recyclerView。当我在recyclerView中按下某个项目时,它会打开另一个活动(该项目的详细信息),而当我按下时,它会将我带到默认片段。
如何返回导航抽屉中的最后一个片段?
这是导航抽屉和详细信息活动的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_recycleview);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
recyclerView = findViewById(R.id.recycler_view);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new TodolistFragment()).commit();
navigationView.setCheckedItem(R.id.nav_todo_list);
FloatingActionButton addBtn = findViewById(R.id.fab);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CustomRecycleview.this, AddTodoActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_todo_list:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new TodolistFragment()).commit();
break;
case R.id.nav_todo_updated:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new TodoUpdatedFragment()).commit();
break;
case R.id.nav_favorite:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new TodolistFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}