0

This seems to work but not sure if I have to add it to every Activity?

android:theme="@style/Theme.AppCompat"

along with extending the Classes with ActionBarActivity

Here is what I did in the manifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.shmira.shmira.MainActivity"
            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=".Book"
            android:theme="@style/Theme.AppCompat"
            android:label="Shmira" >
        </activity>

The class looking like this:

public class Book extends ActionBarActivity



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book);


        getSupportActionBar().setDisplayHomeAsUpEnabled(true); 



@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click
        switch (item.getItemId()) {
            // lets user travel back to where they came from
            case android.R.id.home:
                finish();
                return true;
Lion789
  • 4,402
  • 12
  • 58
  • 96
  • 1
    Where you called `getActionBar().setDisplayHomeAsUpEnabled(true)`? and also apply `` – M D Apr 22 '15 at 11:59
  • possible duplicate of [Android Error \[Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference\]](http://stackoverflow.com/questions/28144657/android-error-attempt-to-invoke-virtual-method-void-android-app-actionbar-on) – Yuva Raj Apr 22 '15 at 11:59
  • It is not the same issue.. – Lion789 Apr 22 '15 at 12:42

3 Answers3

1

If you're using a support lib i.e. ActionBarActivity add the following line after calling setContentView:

  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and if you're not using a support lib i.e. Activity, then simply change getSupportActionBar() to getActionBar().. and don't forget to add android:theme="@style/Theme.AppCompat" to AndroidManifest.xml

To go back to the MainActivity instead of up through the stack, define ActivityA as parent for ActivityB in AndroidManifest.xml like this:

    <activity
        android:name=".Book"
        android:label="Shmira">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.shmira.shmira.MainActivity" />
    </activity>
Musa Y.
  • 1,747
  • 18
  • 26
  • I have to add that theme to every activity in my manifest? And if I am using ActionBarActivity only on the MainActivity I can still extend only Activity for the other activities? ... I still get the error unless I call the theme you mentioned and then use getSupportAction but I have to then extend ActionBarActivity – Lion789 Apr 22 '15 at 12:15
  • You don't have to.. you can set it once in `` attribute, for e.g. ``. For more info http://developer.android.com/guide/topics/manifest/application-element.html – Musa Y. Apr 22 '15 at 12:19
  • Can you explain what does it do now and what is it that you want ? – Musa Y. Apr 22 '15 at 12:58
  • Ok it works thanks... without the parent apparently I was calling finish on creation of the new activity... so one last question so for every activity I have to add this... `android:theme="@style/Theme.AppCompat"` and extend ActionBarActivity ... because that is the only way it seems to work... and I have to use the style in each one it seems this is the pastebin of my style.. http://pastebin.com/S0JWejNg – Lion789 Apr 22 '15 at 13:09
  • Glad it works.. Please accept it as answer if you think my answer helped for the original question. – Musa Y. Apr 22 '15 at 13:12
  • Just noticed your last question... You don't have to set a theme for every activity unless you want all/some activities to have different themes, if you set the theme in `` that's sufficient and will apply that theme in all activities. – Musa Y. Apr 22 '15 at 13:20
  • I know I tried setting it in the application but it errors me afterwards... but I do have to extend every class with ActionBarActivity then? – Lion789 Apr 22 '15 at 13:22
  • Yes, to apply themes you need to have xml file in your activity and extend every class with ActionBarActivity.. otherwise, it won't work. – Musa Y. Apr 22 '15 at 14:02
0
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

In your code your using getActionBar() while base class is ActionBarActivity. You should use above code instead.

Vinoth Kannan
  • 242
  • 5
  • 16
0

Try adding ParentActivity of your Activity in AcivityTag in AndroidManifest.xml

 <activity
        android:name=".Book"
        android:label="@string/book"
        android:parentActivityName="ParentActivity">

Try Adding below code in your oncreate

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
John
  • 8,846
  • 8
  • 50
  • 85
  • Look at the edit, I tried that and it works... but not sure if it is right? Since it does not return it to CreateBook but back to the home page – Lion789 Apr 22 '15 at 12:23
  • Make sure that your parentActivityName should be the one where you started the Book Activity. – John Apr 22 '15 at 12:33
  • I tried that the getActionBar has an error the same as before – Lion789 Apr 22 '15 at 12:38