本文介绍了动作条“向上”按钮,破坏父活动,'回'不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动显示项目的列表相对简单的Andr​​oid应用程序和所选项目的另一个显示细节。我开始名单活动,这是(使用 FLAG_ACTIVITY_CLEAR_TOP 清除登录活动从这个叫)我最顶层的活动:

 意向意图=新的意图(这一点,ListInstancesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(意向);
完();
 

和从该活动我的行为在一个项目中被选择使用:

 意图detailIntent =新的意图(这一点,ShowInstanceActivity.class);
detailIntent.putExtra(ShowInstanceFragment.ARG_ITEM_ID,ID);
startActivity(detailIntent);
 

一切工作正常,如果我用软键返回按钮,然后我回到 ListInstancesActivity 因为我期望的那样。不过,如果不是我preSS的操作栏上的返回/向上按钮,然后它会破坏并重新创建 ListInstancesActivity 。这是不好的,因为它是相对昂贵的计算上这样做。

我如何可以使操作栏的行为以同样的方式为功能键,只是返回previous活动,而不是破坏它。

应该指出的是,我使用的动作条的支持库版本。

的相关部分我的的Andr​​oidManifest.xml

 <活动
  机器人:名称=。agenda.ListInstancesActivity
  机器人:标签=@字符串/ list_instances_activity_title>
< /活性GT;
<活动
  机器人:名称=。agenda.ShowInstanceActivity
  机器人:标签=@字符串/ show_instance_activity_title
  机器人:parentActivityName =agenda.ListInstancesActivity。>
< /活性GT;
 

解决方案

您可以覆盖哪些动作条向上按钮应该做这样的:

 公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){

案例android.R.id.home:
    onBack pressed();
    返回true;
}

返回super.onOptionsItemSelected(项目);
}
 

和重新返回按钮的效果。

I have a relatively simple Android app with one Activity showing a list of items and another showing details of a selected item. I start the list activity, which is my topmost activity (using FLAG_ACTIVITY_CLEAR_TOP to clear the login activity from which this is called) with:

Intent intent = new Intent(this, ListInstancesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

and from within that activity I act on an item being selected with:

Intent detailIntent = new Intent(this, ShowInstanceActivity.class);
detailIntent.putExtra(ShowInstanceFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);

All works fine, and if I use the softkey 'back' button then I return to the ListInstancesActivity as I would expect. However, if instead I press the back/up button on the action bar then it destroys and recreates the ListInstancesActivity. This is bad, as it is relatively computationally expensive to do so.

How can I make the action bar behave in the same way as the softkey and just return to the previous activity rather than destroying it.

It should be noted that I'm using the support library version of the actionbar.

The relevant parts of my AndroidManifest.xml are

<activity
  android:name=".agenda.ListInstancesActivity"
  android:label="@string/list_instances_activity_title">
</activity>
<activity
  android:name=".agenda.ShowInstanceActivity"
  android:label="@string/show_instance_activity_title"
  android:parentActivityName=".agenda.ListInstancesActivity">
</activity>
解决方案

You can override what the actionbar up button should do like:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case android.R.id.home:
    onBackPressed();
    return true;
}

return super.onOptionsItemSelected(item);
}

And recreate the back button effect.

这篇关于动作条“向上”按钮,破坏父活动,'回'不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:51