设置活动如何使用顶部的箭头向后导航

Posted

技术标签:

【中文标题】设置活动如何使用顶部的箭头向后导航【英文标题】:Settings Activity how to navigate back using the arrow at the top 【发布时间】:2017-02-03 02:03:26 【问题描述】:

您好,我创建了一个设置活动,当我在设置菜单中并按下手机的后退按钮时,我会被定向到正确的“主屏幕”,但是当我按下设置的顶部箭头时菜单什么都没有发生,它似乎只是一个按钮。请看 imgur 看看我的意思是哪个箭头。 Back arrow in the settings activity, press here.我查看了java代码,似乎找到了按钮?所有的帮助都是appreciated!!

这是设置菜单的整个java文件。

public class SettingsActivity extends AppCompatPreferenceActivity 
    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() 
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) 
            String stringValue = value.toString();

            if (preference instanceof RingtonePreference) 
                // For ringtone preferences, look up the correct display value
                // using RingtoneManager.
                if (TextUtils.isEmpty(stringValue)) 
                    // Empty values correspond to 'silent' (no ringtone).
                    preference.setSummary(R.string.pref_ringtone_silent);

                 else 
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue));

                    if (ringtone == null) 
                        // Clear the summary if there was a lookup error.
                        preference.setSummary(null);
                     else 
                        // Set the summary to reflect the new ringtone display
                        // name.
                        String name = ringtone.getTitle(preference.getContext());
                        preference.setSummary(name);
                    
                

             else 
                // For all other preferences, set the summary to the value's
                // simple string representation.
                preference.setSummary(stringValue);
            
            return true;
        
    ;


    /**
     * Helper method to determine if the device has an extra-large screen. For
     * example, 10" tablets are extra-large.
     */
    private static boolean isXLargeTablet(Context context) 
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    

    /**
     * Binds a preference's summary to its value. More specifically, when the
     * preference's value is changed, its summary (line of text below the
     * preference title) is updated to reflect the value. The summary is also
     * immediately updated upon calling this method. The exact display format is
     * dependent on the type of preference.
     *
     * @see #sBindPreferenceSummaryToValueListener
     */
    private static void bindPreferenceSummaryToValue(Preference preference) 
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setupActionBar();
    

    /**
     * Set up the @link android.app.ActionBar, if the API is available.
     */
    private void setupActionBar() 
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) 
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        
    

    /**
     * @inheritDoc
     */
    @Override
    public boolean onIsMultiPane() 
        return isXLargeTablet(this);
    

    /**
     * @inheritDoc
     */
    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onBuildHeaders(List<Header> target) 
        loadHeadersFromResource(R.xml.pref_headers, target);
    

    /**
     * This method stops fragment injection in malicious applications.
     * Make sure to deny any unknown fragments here.
     */
    protected boolean isValidFragment(String fragmentName) 
        return PreferenceFragment.class.getName().equals(fragmentName)
                || GeneralPreferenceFragment.class.getName().equals(fragmentName);
    

    /**
     * This fragment shows general preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class GeneralPreferenceFragment extends PreferenceFragment 
        @Override
        public void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            setHasOptionsMenu(true);

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            bindPreferenceSummaryToValue(findPreference("example_text"));




        

        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
            int id = item.getItemId();
            if (id == android.R.id.home) 
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                return true;
            
            return super.onOptionsItemSelected(item);
        
    


    

我认为这是按钮?

 @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setupActionBar();
    

    /**
     * Set up the @link android.app.ActionBar, if the API is available.
     */
    private void setupActionBar() 
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) 
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        
    

【问题讨论】:

【参考方案1】:

打开AndroidManifest.xml 文件。您需要为您的SettingsActivity 定义父Activity 使用:

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

然后在你的班级:

 /**
     * Set up the @link android.app.ActionBar, if the API is available.
     */
    private void setupActionBar() 
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) 
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
        
    

如果您已经在使用带有 Toolbar 的 AppCompatActivity,请查看此解决方案:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// add back arrow to toolbar
if (getSupportActionBar() != null)
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

如果您还想处理此事件或更改后退按钮图标,请转至:Display Back Arrow on Toolbar Android

编辑:将此代码添加到您的活动类:

@Override
public boolean onOptionsItemSelected(MenuItem item)  
        switch (item.getItemId()) 
        case android.R.id.home: 
            onBackPressed();
            return true;
        

    return super.onOptionsItemSelected(item);

希望对你有帮助

【讨论】:

您好,我将数据添加到 androidmanifest 中(请参阅已编辑的问题)。我改变了 actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowHomeEnabled(true); .但是很抱歉,当我按下箭头时,日志猫显示的唯一内容仍然是 action_down 和 action_up(正常按钮按下)。仅此而已:(感谢您的帮助! @Tim 我想问题仍然存在。我编辑了我的答案 哈哈谢谢大家的帮助,我也找到了答案! ***.com/questions/33540497/…我读了这个链接。我用 whooole 代码尝试了谷歌。尽管我总是在问之前先搜索一下网页。感谢您花时间查找所有这些。我很感激。我会投票给你!祝你晚安! :D【参考方案2】:

在您的 AndroidManifest.xml 中,将其添加到 Activity 标记中

<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".HomeScreenActivity"/>

【讨论】:

【参考方案3】:

结合以上答案

SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MainSettingsFragment()).commit();


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    if (item.getItemId() == android.R.id.home) 
        onBackPressed();
    
    return super.onOptionsItemSelected(item);


@Override
public void onBackPressed() 
    super.onBackPressed();
    this.finish();
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);

AndroidManifest.xml

<activity android:name=".SettingsActivity"
    android:label="@string/title_settings">
    <meta-data android:name="android.support.PARENT_ACTIVITY"
    android:value=".MainActivity"/>
</activity>

【讨论】:

【参考方案4】:

你应该试试。

@Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) 
            finish(); // close this activity and return to preview activity (if there is any)`enter code here`
        

        return super.onOptionsItemSelected(item);
    

【讨论】:

在你的 SettingActivity 的 onOptionsItemSelected() ----> startActivity(new Intent(getActivity(), SettingsActivity.class));这条线给你带来了麻烦。

以上是关于设置活动如何使用顶部的箭头向后导航的主要内容,如果未能解决你的问题,请参考以下文章

滑动导航箭头

如何通过后退按钮(箭头)将活动导航到片段?

如何使用不同颜色快速设置导航栏返回箭头和backBarButtonItem标题

(导航组件)返回首页fragment时如何在activity上显示返回箭头?

使用 Ionic v2 离开页面(向后导航)之前发出警报

如何使用按钮在 UIPageViewController 中向前和向后导航?