Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Back Button for previous Activity

I've created an Activity with a Navigation Drawer and replaced the options icon (placed in the top-right corner) with an ImageButton to handle the back click.

The problem is, that I don't know how to do it. I'm a little confused about how to use the back button. What code should I do to go to the previous Activity?

A back button for: Activity to Another Activity and MainActivity to Fragment activity.

this is my Manifest code:

   <activity
        android:name="com.teamamazing.with_sidebar.activity.Accomodation"
        android:label="Accomodation"
        android:parentActivityName="com.teamamazing.with_sidebar.activity.SpecialPage">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.teamamazing.with_sidebar.activity.SpecialPage" />
    </activity>

this is my Accommodation activity:

package com.teamamazing.with_sidebar.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.teamamazing.with_sidebar.R;


public class Accomodation extends AppCompatActivity {

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

and this is my SpecialPage code: which will be the parent activity.

package com.teamamazing.with_sidebar.activity;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.teamamazing.with_sidebar.R;


public class SpecialPage extends AppCompatActivity {

public ImageButton accomodation;


public void init() {
    accomodation = (ImageButton) findViewById(R.id.AccomodationButton);
    accomodation.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent accomodation = new Intent(SpecialPage.this, Accomodation.class);

            startActivity(accomodation);


        }
    });
}


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

    init();
}}

Thank you for the answer.

like image 516
日向愛 Avatar asked May 30 '26 05:05

日向愛


2 Answers

You can use onBackPressed() or finish() Method.

  buttonClickOBJ.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

       onBackPressed();

    }
});

onBackPressed ()

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

like image 170
IntelliJ Amiya Avatar answered Jun 01 '26 19:06

IntelliJ Amiya


You can also use the following method.

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
like image 31
Raymart Prado Avatar answered Jun 01 '26 19:06

Raymart Prado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!