Back button in title bar not working for devices with Lollipop pre-decode

I've added a back button in the header for some of my actions in my app. The back button works great for Lollipop devices, but when I tested my app on an Icecream Sandwich device the back button doesn't work. Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);   //Adding back button
    List<ItemObjectArticle> rowListItem = getAllArticleItemList();
    lLayout = new LinearLayoutManager(ArticleActivity.this);
    RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view1);
    rView.setLayoutManager(lLayout);
    ArticleAdapter rcAdapter = new ArticleAdapter(ArticleActivity.this, rowListItem);
    rView.setAdapter(rcAdapter);
}

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
//noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }else if(id==R.id.home){
        NavUtils.navigateUpFromSameTask(this);  //handling click
        return true;
    }

    return super.onOptionsItemSelected(item);
}

      

And in the android manifest file I added:

<activity
        android:name=".ArticleView"
        android:label="@string/title_activity_article_view"
        android:parentActivityName="com.example.android.kheti.ArticleActivity">   //this
    <intent-filter>
        <action android:name="com.example.android.kheti.ARTICLEVIEW" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

      

What code do I need to change to make it work on all Android devices?

+3


source to share


1 answer


From the docs

As of Android 4.1 (API Level 16), you can declare a boolean parent of each activity by specifying the android: parentActivityName attribute on the element.

If your app supports Android 4.0 and below, enable Library support with your app and add an element inside, then specify the parent activity as the value for android.support.PARENT_ACTIVITY corresponding to android: parentActivityName.



So, you need to add metadata to make it work on all devices. Add support library and try with this:

<activity
        android:name=".ArticleView"
        android:label="@string/title_activity_article_view"
        android:parentActivityName="com.example.android.kheti.ArticleActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.kheti.ArticleActivity" />
 </activity>

      

+1


source







All Articles