kirktoon1882
kirktoon1882

Reputation: 1231

Action bar setDisplayHomeAsUpEnabled closes app

My app uses the android.support.v7.app.ActionBar so that I can use the Action Bar on my Android 2.3.4 phone and my tablet using Android 4.4.2. I've got my minimum and target SDK's set at 10 and 18. When I click the back button on the Action Bar which calls setDisplayHomeAsUpEnabled(true) it works correctly on the 2.3.4 phone and goes back to the Home Activity called Main_Interface. But when I click the back button on the Action Bar on the tablet, it closes the app as if I clicked the back button on the tablet. Nothing comes up in LogCat, so it's acting as though it's supposed to close the app when the Action Bar setDisplayHomeAsUpEnabled() button is clicked. Here's what the Activity looks like in the Manifest:

<activity
    android:name=".UILNPPager"
    android:configChanges="orientation|screenSize"
    android:label="UILNPPager"
    android:parentActivityName="com.myapp.ktg.Main_Interface"
    android:theme="@style/Theme.Blu_uilpager" >
    <intent-filter>
        <action android:name="com.myapp.ktg.UILNPPAGER" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.myapp.ktg.Main_Interface" />
</activity>

Here's the Activity stripped of everything but the ActionBar calls:

    import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class UILNPPager extends ActionBarActivity {
private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
String[] imTitlesStr;
String[] imMediumStr;
String[] imPriceStr;
String[] imageUrls;
int pagerPosition;
PhotoViewAttacher mAttacher;

ActionBar mActionbar;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionbar_uilpager);

    //--- ViewPager stuff
     ....
    //--- END ViewPager stuff

    /** Getting a reference to action bar of this activity */       
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    return true;
}

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

}

I tried taking the call android:parentActivityName="com.myapp.ktg.Main_Interface" out of the manifest, but that has no effect. It still works correctly on the 2.3.4 phone and not on the 4.4.2 tablet.

Any idea why it doesn't work right on the tablet?

Upvotes: 2

Views: 6025

Answers (2)

kirktoon1882
kirktoon1882

Reputation: 1231

In my app I'm using my own transitions to move from Activity A to Activity B. I've discovered that the call overridePendingTransition(); in the onPause(); call in Activity A breaks the getSupportActionBar().setDisplayHomeAsUpEnabled(true); call in Activity B, which has the ActionBar. So on Activity A, I removed the onPause() method and called overridePendingTransition(); in the Intent that calls Activity B, like this:

    Intent miI = new Intent();
miI.setClass(Splash.this, Main_Interface.class);
startActivity(miI);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);

Then in Activity B I removed the onPause(); method and added overridePendingTransition(R.anim.fadein, R.anim.fadeout); to the onCreate(); method like so:

        super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.mainpage);

Then I put the overridePendingTransition(); call in all other Activities' onCreate(); methods just like in Activity B. Doing this makes the transition work correctly. And to make the ActionBar go back to the Main_Interface Activity correctly on my 4.4.2 tablet, I just call getSupportActionBar().setDisplayHomeAsUpEnabled(true);. And everything is working correctly!

Upvotes: 0

Gavin
Gavin

Reputation: 4698

Please use a AppCompat theme or a theme that inherits one of them. For example:

  1. @style/ThemeAppCompat
  2. @style/ThemeAppCompat.Light
  3. @style/ThemeAppCompat.Light.DarkActionBar

I noticed that you are using your own styled theme, you can inherit the theme from one of the AppCompat theme as follow.

<style name="Theme.Blu_uilpager"
       parent="@style/ThemeAppCompat.Light">
     ....
</style>

Quoted from a official Android tutorial, https://developer.android.com/training/basics/actionbar/styling.html

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.

Upvotes: 1

Related Questions