KNS
KNS

Reputation: 101

Why is the Up button behaving like back?

A. What I did :

  1. I created a new app (to make sure there wasn't anything wrong with my code in the app I'm working on that made this happen) with 2 activities: Activity 1 (launch activity) and Activity 2

  2. I added ONE LINE to the manifest: android:parentActivityName=".Activity1"> It now looks like :

    <activity
        android:name=".Activity1"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity2"
        android:label="@string/title_activity_activity2"
        android:parentActivityName=".Activity1">
    </activity>
    
  3. Activity 1 has a button with this method to show Activity 2:

    public void goToPage(View v){
        Intent mainIntent = new Intent(Activity1.this, Activity2.class);
        Activity1.this.startActivity(mainIntent);
        Activity1.this.finish();
    }
    
  4. Run the app. Press the button --> I've went from A1 to A2. Then I press the Up button on the bar and go back to the home screen !

  5. I add this to onCreate in Activity 2:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    

Run the code again, same behaviour.

I'm following android's documentation (http://developer.android.com/training/implementing-navigation/ancestral.html)

Thank you for helping

Upvotes: 0

Views: 49

Answers (1)

amodkanthe
amodkanthe

Reputation: 4530

here is code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            break;
        }

    return true;
}

Now on up button if parent activity is there in activity stack up will take user to parent activity otherwise it will work as back.

so in your case try commenting following line your code.

Activity1.this.finish(); 

Upvotes: 1

Related Questions