1

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.

Janvi Vyas
  • 732
  • 5
  • 16
Kazi Kowshik
  • 95
  • 1
  • 3
  • 7
  • use `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` – Jyoti JK Apr 28 '18 at 08:00
  • Possible duplicate of [How to display and set click event on Back Arrow on Toolbar](https://stackoverflow.com/questions/35810229/how-to-display-and-set-click-event-on-back-arrow-on-toolbar) – Jyoti JK Apr 28 '18 at 08:00

4 Answers4

5

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

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
  • Nice, Its display !! But it's can't working. When I click back icon nothing happen. I need to when click back icon I will back my previous activity. Please help me. – Kazi Kowshik Apr 28 '18 at 09:09
  • This is for main activity. when press button go to new activity. button = (Button) findViewById(R.id.login); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent myIntent = new Intent(MainActivity.this, NewActivity.class); startActivity(myIntent); } }); – Kazi Kowshik Apr 28 '18 at 09:13
4

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"/>
Rishabh Sharma
  • 270
  • 5
  • 17
1

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)
    }
1

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

Aathil Ahamed
  • 460
  • 4
  • 16