在所有活动中使用通用工具栏时,onbackpress返回上一个活动的上一个

时间:2019-05-08 08:30:06

标签: android menu navigation-drawer toolbar onbackpressed

我在应用程序中使用导航抽屉,并在所有活动中使用相同的工具栏。

在主页中,我正在打开意图,然后在FirstActivity.java中再次转到FirstActivity.java类。 首页-> FirstActivity.java-> SecondActivity.java

问题是,当我尝试单击工具栏菜单时,会从我的第二活动中打开一个新活动 CartActivity.java ,并且当我按下 CartActivity.java 而不是返回第二活动,而是返回第一活动

当我从我的第一个活动打开 CartActivity.java 并按下 CartActivity.java 的后退按钮时,发生了同样的事情返回第一个活动的操作,然后返回首页。

  

我认为因为这样我的布局中包含了工具栏   在每个活动中 ,并且由于菜单选项位于每个工具栏中,因此它会返回到其   上一个工具栏

     

还是没有人知道任何方法来显示全部相同的工具栏   像 Myntra 应用程序中那样的活动,每个工具箱中都存在相同的工具栏   活动,当我们单击购物车选项时,它将打开一个新活动   在关闭购物车活动时,它会返回到前一个   活动。

OptionsMenuActivity.java

public class OptionsMenuActivity extends AppCompatActivity {

    @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) {
        int id = item.getItemId();

        if (id == R.id.action_cart) {

            Intent intent = new Intent(getApplicationContext(), CartActivity.class);
            startActivity(intent);
            return true;
        }
 return super.onOptionsItemSelected(item);
    }
}

CartActivity.java

public class CartActivity extends AppCompatActivity {
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        toolbar=findViewById(R.id.toolbarcustom);
        setSupportActionBar(toolbar);
        toolbar.setTitle("cart");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        onBackPressed();
        return super.onOptionsItemSelected(item);
    }
}

MainActivity.java

public class MainActivity extends OptionsMenuActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        txt=findViewById(R.id.txt);
        txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(), FirstActivity.class);
                startActivity(intent);
            }
        });


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout 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);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

        if (id == R.id.nav_home) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } 

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

FirstActivity.java

public class FirstActivity extends OptionsMenuActivity {

    Toolbar toolbar;
    TextView txtfirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        toolbar=findViewById(R.id.toolbarcustom);
        setSupportActionBar(toolbar);
        toolbar.setTitle("first");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        txtfirst=findViewById(R.id.txtfirst);
        txtfirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      onBackPressed();


        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案