简体   繁体   English

如何在ActionBar AppCompatActivity Android Studio中创建后退按钮

[英]How to create a back button in ActionBar AppCompatActivity Android Studio

I created a simple Navigation Drawer Activity and a Basic Activity which I named of LoginActivity , so when I select a item from my Drawer i was able to navigate to my LoginActivity but there's no BackButton in my ActionBar, I came from Xamarin Forms and there, depending on navigation type, the back button was visible! 我创建了一个简单的Navigation Drawer Activity和一个Basic Activity ,并将其命名为LoginActivity ,所以当我从Drawer中选择一个项目时,我能够导航到LoginActivity,但是ActionBar中没有BackButton,我来自Xamarin Forms,根据导航类型,后退按钮是可见的! So far I have : 到目前为止,我有:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar); ................

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);

Android Studio 2.2.2
compileSdkVersion 24

Change this code in your onCreate method: 在您的onCreate方法中更改以下代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.YOUR_NAVIGATION_ICON); //This is the line to add

Furthermore, you can determine what clicking this icon does by: 此外,您可以通过以下方式确定单击此图标的作用:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                //Whatever your require the navigation to do
                return true;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM