如何在Android中的导航抽屉中实现后退按钮?我实现了一个包含多个片段(1-7)的导航抽屉。但是我不知道如何启用应该切换到上一个片段的后退按钮。
这个包含导航抽屉代码的主要活动....在定义4个片段....我知道如何实现后退按钮
Mainactivity.java:
package com.example.admin.realstate;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
CollapsingToolbarLayout collapsingToolbarLayout;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(savedInstanceState==null)
{
Home_Fragment fragment=new Home_Fragment();
FragmentManager frgmentmanager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=frgmentmanager.beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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);
}
@Override
public void onBackPressed()
{
DrawerLayout drawer = (DrawerLayout) 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;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
item.setChecked(true);
// Set action bar title
setTitle(item.getTitle());
if (id == R.id.nav_Home)
{
Home_Fragment fragment=new Home_Fragment();
FragmentManager frgmentmanager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=frgmentmanager.beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.addToBackStack("home_fragment");
fragmentTransaction.commit();
}
else if (id == R.id.nav_project)
{
Projects_Fragments fragment=new Projects_Fragments();
FragmentManager frgmentmanager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=frgmentmanager.beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.addToBackStack("project_fragment");
fragmentTransaction.commit();
}
else if (id == R.id.nav_amenities)
{
Amenities fragment=new Amenities();
FragmentManager frgmentmanager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=frgmentmanager.beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.addToBackStack("amenities_fragment");
fragmentTransaction.commit();
}
else if (id == R.id.nav_contactsus)
{
Contactus_Fragment fragment=new Contactus_Fragment();
FragmentManager frgmentmanager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=frgmentmanager.beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.addToBackStack("contact_fragment");
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
}