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;
3

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

8 Comments

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
You don't have to.. you can set it once in <application> attribute, for e.g. <application .... android:theme="@style/Theme.AppCompat">. For more info developer.android.com/guide/topics/manifest/…
Can you explain what does it do now and what is it that you want ?
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.. pastebin.com/S0JWejNg
Glad it works.. Please accept it as answer if you think my answer helped for the original question.
|
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);

3 Comments

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
Make sure that your parentActivityName should be the one where you started the Book Activity.
I tried that the getActionBar has an error the same as before
0
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

1 Comment

use getSupportActionBar() for ActionBarActivity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.