[英]Proper way to handle action bar up button?
我使用 ActionBarSherlock(虽然我认为这并不重要)。
我有一个主要活动和一个关于活动。 我希望关于活动通过其徽标显示后箭头,并执行适当的动画等。 我不知道如何正确地做到这一点。
目前,当按下向上/主页按钮时,我在 onOptionsMenuItemSelected 下启动 Main 活动,但它很笨拙并且不能正常工作。 它播放错误的动画,并且无法很好地处理多任务处理。
我该如何正确设置?
这是我的主要活动中启动关于的部分:
Intent aboutIntent = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(aboutIntent);
这是我的关于活动:
package com.stevenschoen.test;
import android.content.Intent;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
public class About extends SherlockActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intentHome = new Intent(this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentHome);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
您是否也尝试过此操作(取自此处的Android 网站):
在清单中,对于需要转到主活动的每个活动 X,将其添加到代码中:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
并将其添加到其清单 xml 标记中:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" />
如果您仍然需要在主要活动上保持相同的状态,请改用以下代码:
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, intent);
如果API为16及以上,可以添加parentActivityName属性,路径为main Activity,而不是metadata。
发现我的问题的根源是我不久前所做的清单中的更改:我添加了这一行:
android:launchMode="singleInstance"
所以我的主要活动不会重新启动。 将其更改为:
android:launchMode="singleTask"
解决了我的问题,我能够删除所有onOptionsItemSelected
东西。 我知道有问题,Android 应该能够更好地处理这个问题,我是对的。 检查清单:P
您可以使用代码或 XML 处理操作栏按钮。
检查此代码
如果你想以编程方式在onCreate()方法中添加这一行
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
并覆盖此方法onSupportNavigateUp()
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
或以非编程方式,您可以将元添加到清单文件中
<activity android:name="Current Activity"
android:parentActivityName="Activity you want to open">
</activity>
注意:确保 Actionbar 不为空。
在你的onCreate(Bundle savedInstanceState)
,做
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
然后在你的onOptionsItemSelected(MenuItem item)
,做
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// go to previous screen when app icon in action bar is clicked
Intent intent = new Intent(this, PreviousActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
对于只想回到上一个活动的人来说,这是最简单的解决方案:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish(); //this method close current activity and return to previous
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings2);
//here is to put the up back button in the Activity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
// here is the code to handle to go back to the activity which sent you to this current one
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
确保您的 android:minSdkVersion="11" 可以在清单文件中看到,APK 11 中已包含向上图标。我制作了一个小示例,请尝试以下链接,这可能有助于您导入到您的工作空间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.