无法在仪表板活动中的supportActionbar上实现后退箭头

时间:2018-09-10 14:26:44

标签: android android-fragments android-toolbar

我无法在工具栏上显示后退按钮。我有一个主要活动仪表板活动和许多片段。如果在后堆栈中有片段,我想在工具栏上显示后退按钮。我如何在这里实现它是我的dashboardActivity.java类文件。请告诉我是否是实现片段或仅可管理的最佳方法。我还想在工具栏的右侧添加社交共享图标。这种工具栏是否可行。我删除了导入语句进行问题分类。任何帮助将不胜感激。

            package com.example.narmail.MyApp.Api.Activities;


        public class DashboardActivity2 extends AppCompatActivity implements OnNavigationItemSelectedListener {

            public static Activity activity = null;
            public String token = null;
            public static Boolean truck = false;
            NavigationView navigationView;
            DrawerLayout drawerLayout;
            Toolbar toolbar;
            ActionBarDrawerToggle actionBarDrawerToggle;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                final AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.7F);
                buttonClick.setDuration(300);

                activity = this;

                setContentView(R.layout.activity_dashboard);
                setToolbar();
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                navigationView = findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);
            }

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        Intent a = new Intent(DashboardActivity2.this, DashboardActivity2.class);
                        startActivity(a);
                        // fragment = new DashboardFragment();
                        break;
                    case R.id.nav_post_load:
                        fragment = new PostLoadFragment();
                        break;
                    case R.id.nav_add_truck:
                        fragment = new AddTruckFragment();
                        break;
                    case R.id.nav_manage_truck:
                        fragment = new ManageTruckFragment();
                        break;
                    case R.id.nav_manage_load:
                        fragment = new ManageLoadFragment();
                        break;
                    case R.id.nav_signout:
                        signoutAlert();
                        break;

                }
                if (fragment != null) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.screen_area, fragment).addToBackStack(null).setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_right).commit();
                } else {
                    fragment = new DashboardFragment();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.screen_area, fragment).commit();
                }
                drawerLayout = findViewById(R.id.drawerLayout);
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }

            private void setToolbar() {
                drawerLayout = findViewById(R.id.drawerLayout);
                toolbar = findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                //  getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arraow);
                toolbar.setNavigationIcon(R.drawable.back_arraow);
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.w("185 ", "back button pressed");
                        // back button pressed
                    }
                });

                actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
                drawerLayout.addDrawerListener(actionBarDrawerToggle);
                actionBarDrawerToggle.syncState();
            }

            // Before 2.0
            public void onBackPressed() {
                if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getSupportFragmentManager().popBackStack();
                } else {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    startActivity(intent);
                }
            }

            public void signoutAlert() {
                new AlertDialog.Builder(this)
                        .setMessage("Are you sure you want to Signout?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                UserApi.logOut(DashboardActivity2.this);
                            }
                        })
                        .setNegativeButton("No", null)
                        .show();
            }

        }

这是dashboardActivity.xml文件

<?xml version="1.0" encoding="utf-8"?>
        <android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
           <LinearLayout
                android:id="@+id/my_screen"
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent">
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:background="@color/colorPrimary"
                    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
                    android:layout_height="wrap_content">

                </android.support.v7.widget.Toolbar>
               <include layout="@layout/content_main">
               </include>
            </LinearLayout>
           <android.support.design.widget.NavigationView
               android:id="@+id/nav_view"
               android:layout_width="wrap_content"
               android:layout_gravity="start"
               app:menu="@menu/drawer_menu"
               app:headerLayout="@layout/header"
               android:layout_height="wrap_content">
           </android.support.design.widget.NavigationView>

        </android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

尝试一下-

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

然后您可以通过实现以下内容

来覆盖后退图标的行为:
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                //Or do your Interesting Stuff here! :)
        }
        return true;
    }