如何在Android Studio中的ActionBar上添加后退按钮?

0qx6xfy6  于 2023-01-07  发布在  Android
关注(0)|答案(5)|浏览(355)

我创建了一个应用程序,希望在操作栏上有一个后退按钮,以便使用Android Studio导航回上一页。我查看了许多示例,但在setDisplayHomeAsUpEnabled下不断出现错误

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

public boolean onOptionsItemSelected(MenuItem item) {
    Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivityForResult(myIntent, 0);

    // 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 id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}
tpgth1q7

tpgth1q71#

假设您有一个DetailActivity,并且需要返回到MainActivity的按钮。首先,将DetailActivity的清单更改为

<activity
    android:name=".DetailActivity"
    android:label="@string/title_activity_detail"
    android:parentActivityName=".MainActivity">
  <meta-data
      android:name="android.support.PARENT_ACTIVITY"
      android:value="com.example.MainActivity"/>
</activity>

以及详细活动的onCreate

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

可以解决问题。这是最简单的实现。

0yg35tkg

0yg35tkg2#

onCreate()中使用此选项

ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);

而不是将此侦听器添加到MainActivity.java类中

protected OnBackPressedListener onBackPressedListener;

    public interface OnBackPressedListener {
        void doBack();
    }

    public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
        this.onBackPressedListener = onBackPressedListener;
    }

    @Override
    public void onBackPressed() {
        if (onBackPressedListener != null)
            onBackPressedListener.doBack();
        else
            super.onBackPressed();
}

现在在Fragment类中,您可以实现MainActivity.OnBackPressedListener和:

@Override
public void doBack() {
    //Do on back pressed operation
}
nwlls2ji

nwlls2ji3#

使您的活动扩展AppCompatACtivity,然后将navigationIconclicklistener沿着设置为toolbar
例如

public class MyActivity extends AppCompatActivity {

    Toolbar toolbar;

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back_arrow); // Set the icon

        // Icon click listener
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                Log.d("cek", "home selected");
            } 
        }); 
    }
}

确保您的活动主题为AppCompat

<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customization will goes here -->
</style>
siotufzp

siotufzp4#

//In Kotlin
// go to AndroidManifest.xml file apply this change

<activity android:name=".PresentActivity">
            // for adding back button on top
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".BackActivity"/>
        </activity>

// Hope this will work.
fae0ux8s

fae0ux8s5#

//对于操作栏上的添加后退按钮==================//将此添加到onCreate方法中..........

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

//将此添加到onCreate捆绑包的下面=======

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item){

    if (item.getItemId()==android.R.id.home){

        this.finish();
    }
    return super.onOptionsItemSelected(item);
}

相关问题