简体   繁体   中英

Action Bar Home Logo/Button/Icon

In my app, the user should be able to tap on the app icon on the action bar to go back to the very first activity. I have tried to implement the following code but it doesn't seem to work. Anybody has any suggestions to make it work?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_details);
    ActionBar actionBar = getActionBar();
    actionBar.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.layout.menu, menu);
    return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent intent = new Intent(this, First.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;    
    case R.id.feedback:
        Intent intent1 = new Intent(ResultDetails.this.getApplicationContext(), feedback.class);
        ResultDetails.this.startActivity(intent1);
        return (true);

    case R.id.about:
        Intent intent2 = new Intent(ResultDetails.this.getApplicationContext(), about.class);
        ResultDetails.this.startActivity(intent2);            
        return (true);

    }

    return (super.onOptionsItemSelected(item));
}

you should call

actionBar.setDisplayHomeAsUpEnabled(true);

in onCreate() to activate the back button.

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