我在我的应用程序中为我的一些活动在标题栏上添加了一个后退按钮。后退按钮适用于Lollipop设备但是当我在冰淇淋三明治设备上测试我的应用程序时,后退按钮不起作用。这里'我的代码:
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);
}
在Android Manifest文件中,我添加了:
<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>
我需要更改哪些代码才能使其适用于所有Android设备?
答案 0 :(得分:1)
来自文档
从Android 4.1(API级别16)开始,您可以声明逻辑 通过指定android:parentActivityName,每个活动的父级 元素中的属性。
如果您的应用支持Android 4.0及更低版本,请添加支持 使用您的应用程序库并在其中添加元素 。然后将父活动指定为值 android.support.PARENT_ACTIVITY,匹配 android:parentActivityName属性。
因此,您需要添加元数据,以使其在所有设备中都能正常运行。 添加支持库并尝试使用:
<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>