简体   繁体   English

从操作栏菜单项单击调用活动

[英]Call Activity from Action Bar menu item click

I have 1 Fragment and 2 activities: HomeFragment , ProductActivity , and CartActivity .我有 1 个 Fragment 和 2 个活动: HomeFragmentProductActivityCartActivity I want to call CartActivity whenever I click on the cart in the menu.每当我单击菜单中的购物车时,我都想调用CartActivity But when I tap on 1 product in HomeFrament , my app calls ProductActivity and CartActivity under ProductActivity .但是,当我在HomeFrament中点击 1 个产品时,我的应用会在ProductActivity下调用ProductActivityCartActivity

I am using implementation androidx.appcompat:appcompat:1.0.2 .我正在使用实现androidx.appcompat:appcompat:1.0.2

In Product.java, I call Cart Activity in onOptionsItemSelected在 Product.java 中,我在 onOptionsItemSelected 中调用 Cart Activity

// Click on option button.
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    switch (id){
        case android.R.id.home: {
            this.finish();
        }
        case R.id.app_bar_cart: {
            Log.d(TAG,"Cart Clicked");
            Intent intent = new Intent(this, Cart.class);
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

My manifest file:我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.project.coconuc">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".Cart"/>
        <activity android:name=".product" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

onClick call products onClick来电产品

    @Override
    public void onClick(Integer position) {
        Intent callProductActivity = new Intent(mfragment.getActivity(), product.class);
        callProductActivity.putExtra("variable", position.toString());
        Objects.requireNonNull(mfragment.getActivity()).startActivity(callProductActivity);
    }

For more details, please see this image: https://ibb.co/mFFZC30有关更多详细信息,请参阅此图像: https://ibb.co/mFFZC30

This is my code: https://github.com/KhoaChau0594/MoblileAppProject/tree/temp这是我的代码: https://github.com/KhoaChau0594/MoblileAppProject/tree/temp

I have solved my problem.我已经解决了我的问题。 It caused because the implement of the toolbar back button in the product activity.这是由于产品活动中工具栏后退按钮的实现引起的。 I have to use getSupportActionBar() instead of getActionBar().我必须使用 getSupportActionBar() 而不是 getActionBar()。

This is how i implement back button这就是我实现后退按钮的方式

        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

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

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