Search code examples
androidback

Back button in android studio


Hi guys i try to implement a back button in android studio that will bring me back to my previous activity.I manage to display the button on my app but when i press the button it just don't function .There is no error in my program.

 package com.example.window8.myfriend;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class design extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_design);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setLogo(R.drawable.images);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

}
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_image, menu);

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    //noinspection SimplifiableIfStatement
    if (id == R.id.home) {

        Intent intent = new Intent(getApplicationContext(),Systemtime.class);
        startActivity(intent);

        return true;


    }

    return super.onOptionsItemSelected(item);
}

}


Solution

  • Back button should be android.R.id.home & below line will be used to return to parent activity.

    NavUtils.navigateUpFromSameTask(this);