-1

I'm new to Android and I trying to do a simple app.

In the "home", I write my name and press the "send" button.
In the second page, the app show "Hello " + inserted name.

The problem is:
When I touch the back virtual button in nav bar, I come back to the home and in the input field there is always the name; but when I go back with the back button in header bar, the input field is empty.

problem

I've tried with:

in onOptionsItemSelected:

    switch (item.getItemId()){
        case android.R.id.home:
            Log.d("case","go back");
            NavUtils.navigateUpFromSameTask(this); //or with finish();
            return true;
    }

it prints "case: go back" in console, but doesnt' work correcly.

My custom theme have as parent Theme.AppCompat.Light.DarkActionBar and if i put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreate, Android Studio tells me "Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'".

Simone Sessa
  • 795
  • 1
  • 12
  • 35

1 Answers1

0

I solved adding this in the second Activity:

@Override
public Intent getSupportParentActivityIntent() {
    return null;
}

and edit the switch/case function like this:

switch (item.getItemId()){
    case android.R.id.home:
        this.finish();
        return false;
    default:
        return super.onOptionsItemSelected(item);
}
Simone Sessa
  • 795
  • 1
  • 12
  • 35