Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 操作操作栏向上按钮的正确方法?_Java_Android_Android Actionbar - Fatal编程技术网

Java 操作操作栏向上按钮的正确方法?

Java 操作操作栏向上按钮的正确方法?,java,android,android-actionbar,Java,Android,Android Actionbar,我使用ActionBarSherlock(尽管我认为这无关紧要) 我有一个主要活动和一个关于活动。我希望About活动通过其徽标显示后箭头,并进行适当的动画等。 我不知道该怎么做 目前,我在OnOptions菜单项下选择了在按下“向上/主页”按钮时启动主活动,但它很粗糙,不能正常工作。它播放错误的动画,并且处理多任务的能力很差 我该如何正确设置 以下是我的主要活动中发布的关于: Intent aboutIntent = new Intent(MainActivity.this, About.cl

我使用ActionBarSherlock(尽管我认为这无关紧要)

我有一个主要活动和一个关于活动。我希望About活动通过其徽标显示后箭头,并进行适当的动画等。 我不知道该怎么做

目前,我在OnOptions菜单项下选择了在按下“向上/主页”按钮时启动主活动,但它很粗糙,不能正常工作。它播放错误的动画,并且处理多任务的能力很差

我该如何正确设置

以下是我的主要活动中发布的关于:

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);
        }
    }
}
onCreate(Bundle savedInstanceState)
中,执行以下操作

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
@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);
}
然后在您的
onOptions ItemSelected(菜单项)
中,执行以下操作

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
@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);
}

发现我的问题的根源是我前一段时间在清单中所做的更改:我添加了以下行:

android:launchMode="singleInstance"
所以我的主要活动不会重新启动。将其更改为:

android:launchMode="singleTask"

解决了我的问题,我能够删除所有选项ItemSelected内容。我知道有点不对劲,安卓应该能够更好地处理这个问题,我是对的。检查清单:P

确保清单文件中显示的安卓:minSdkVersion=“11”,APK 11中包含了Up图标。我已经做了一个小样本plz尝试下面的链接,这可能会帮助你只是导入到你的工作空间

您也尝试过这个(摘自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);
}
它的manifest xml标记是:

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" />

如果API为16或更高,则可以使用主活动的路径而不是元数据添加属性。

对于只想返回上一个活动的用户,这是最简单的解决方案:

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);
}

您可以使用代码或XML处理操作栏按钮

检查此代码

如果希望以编程方式将这一行添加到onCreate()方法中

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
并在SupportNavigateUp()上重写此方法

或者以非编程方式将元数据添加到清单文件中,如下所示:

<activity android:name="Current Activity"
        android:parentActivityName="Activity you want to open">
    </activity>


注意:确保Actionbar不为空。

这正是我所拥有的。它的功能不像谷歌的“向上”按钮。它播放启动新活动(而不是返回上一个活动)的动画。。。。这对我来说很好。也许你应该发布你的代码?是否导入正确的
菜单项
?您是否正在扩展Sherlock活动?我不知道还有什么好建议haha@D_Steve595我也有同样的问题。你有没有想过如何使动画与正常的后退按钮相同?因此,使用你发布的代码将清空除主活动(也是第一个)之外的所有活动?Android文档说“其他模式(singleTask和singleInstance)不适用于大多数应用程序,因为它们会导致用户可能不熟悉的交互模型,并且与大多数其他应用程序非常不同。“Api级别11之前的
可用
谷歌支持
使用
向上图标
。您可以调用
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
。如果它是一个片段呢?怎么做?@user2056563类似的方法。在需要活动时使用“getActivity”,在片段的“onCreate”中也使用“SetHasOptions菜单(true)”。不要忘记为片段的“onCreateOptionsMenu”方法调用“super.onCreateOptionsMenu”(我在末尾调用它,但我认为在开头也可以)。
从Android 4.1(API级别16)开始,您可以通过在,所以我要纠正最后一点info@Bhargav有什么需要纠正的?如果您使用支持库,则使用我提供的元数据标记mentioned@Bhargav哦,你是说这是API 16而不是11。好的,我会解决这个问题。谢谢
@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);
}