0

I want to create a Back "Button" in my Android app.

I have the standard Layout in the Eclipse project, with the small image in the left top corner. Now i want to open a new activity and when i click on this image, i want to get back to my activity before (like parent and children)

For example: The way on the ESPN ScoreCenter app:

Home Screen Home Screen / Parent Screen

Back Screen (This is the Screen i want) Back Screen / Child Screen

Is this possible with two different Activitys?

  • 1
    call `finish()` onclick of your back button. It is not clear, when a button behave like back buton and Home button, will you clarify – Pragnani May 12 '13 at 09:51
  • i don't know how to make this back button, that should be my question... :) –  May 12 '13 at 09:51
  • 1
    http://developer.android.com/training/implementing-navigation/ancestral.html Read and try it – Michael Schmidt May 12 '13 at 10:10

3 Answers3

2

Use setHomeAsUpEnabled() to show the back arrow. Override onOptionsItemSelected to end the activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return(true);
    }

    return(super.onOptionsItemSelected(item));
}
nhaarman
  • 98,571
  • 55
  • 246
  • 278
1

You just have to set listener on the button/imageView you are using as back. And inside that listener just call this.finish(). It will destroy the current activity and will take you on the previous/parent activity which started the current activity.

Example :

void onBack(View v){
// Will destroy current activity and take you to the previous activity which started this one
// Thus will act like back button 
this.finish();
}
Vipul J
  • 6,641
  • 9
  • 46
  • 61
0

I suggest you to read the full article describing the ActionBar available on the Android developer guide: http://developer.android.com/guide/topics/ui/actionbar.html

Particularly this chapter explaining in details the functionality you are asking and how to set it up: http://developer.android.com/guide/topics/ui/actionbar.html#Home

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85