Kazi Kowshik
Kazi Kowshik

Reputation: 105

How to display back button on action bar in android another activity

I want to need back button on action bar when I move another activity from main activity. Please help me how can I do now.

I am new in android development, please explain something details.

Thank you.

Upvotes: 1

Views: 7012

Answers (4)

Aathil Ahamed
Aathil Ahamed

Reputation: 474

You can enable the back button in Activity bar by mentioning parent activity in Android Manifest file. For Example I have to enable back button bar in the Activity bar of AddNotes activity in my project. When the user clicks it should redirect the user to the Main Activity, so in the Android manifest file I set the parent activity like this to the AddNotes activity.

<activity android:name=".AddNotes" android:parentActivityName=".MainActivity"></activity>

It will enable back button in the Add note activity's activity bar.

Or if you yo can enable the back button in the activity bars like this also,

val actionbar = supportActionBar
actionbar!!.title = "Add Note"
actionbar.setDisplayHomeAsUpEnabled(true)
actionbar.setDisplayHomeAsUpEnabled(true)

(This codes are Kotlin codes)

For more knowledge please refer: https://developer.android.com/training/appbar/up-action

Upvotes: 1

Nikunjkumar Kapupara
Nikunjkumar Kapupara

Reputation: 356

To display Android System Back Button on Action bar.. if your are using androidx library then it's better to use

supportActionBar instead of actionBar in Kotlin

Full code for showing and handling Android System Back Button:

supportActionBar?.apply {
            setDisplayHomeAsUpEnabled(true)
            title = "SET YOUR ACTIVITY TITLE"
        }

How to close Activity when System Back Arrow button clicked:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

Upvotes: 1

letsintegreat
letsintegreat

Reputation: 3366

Just add code this in the onCreate method of your [CurrentActivity].java file.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And this line of code will just add a back button in your Action Bar, but nothing would happen after tapping that right now.

And add this in your [CurrentActivity].java, this will add the working of that button:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:

            Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

And replace CurrentActivity to your activity name and replace MainActivity to the activity you want to send user after pressing back button

Upvotes: 4

Rishabh Sharma
Rishabh Sharma

Reputation: 268

You can do it like this. In your AndroidManifest.xml file you can tell your activity that what is his parent activity. Here MainActivity is the parent activity of SecondActivity. SecondActivity will have back button which when pressed will take user to MainActivity.

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".SecondActivity" 
    android:parentActivityName=".MainActivity"/>

Upvotes: 3

Related Questions