我已经编写了一段代码作为应用程序的一部分,我想在操作栏/工具栏上实现一个后退按钮,这样当按下按钮时,前一页(紧接在当前页面/片段之前的页面/片段) )将显示.
这是ToolBar,DrawerLayout,NavigationView和getSupportActionBar()的代码:
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Run Code Online (Sandbox Code Playgroud)
我无法使用ActionBar.出于某种原因(我不知道为什么),我的Android工作室/程序,不允许我使用ActionBar.所以我用set/getSupportActionBar()替换它.
与此相关的函数是:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, 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 …Run Code Online (Sandbox Code Playgroud)