简体   繁体   中英

How to add back button on ActionBar in Android Studio?

I have created an app and wanted a back button on my action bar to navigate back to the previous page using Android Studio. I have looked at a number of examples but keep getting errors under 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);

}

Assuming that you have a DetailActivity and you need back button to MainActivity. First change your manifest to for 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>

and in onCreate of DetailActivity

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

will fix things. This is the simplest implementation.

Use this in onCreate()

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

Than add this listener in your MainActivity.java class

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();
}

So now in your Fragment class you can implements MainActivity.OnBackPressedListener and than:

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

Make your activity to extends AppCompatACtivity and then set navigationIcon to your toolbar along with clicklistener

For example

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");
            } 
        }); 
    }
}

Make sure your activity theme is AppCompat

<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customization will goes here -->
</style>
//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.

// For add back button on action bar================== //add this in onCreate method..........

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

//add this below of onCreate Bundle ========

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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