背部和导航向上会有不同的结果 [英] Back and Nav Up have different results

查看:129
本文介绍了背部和导航向上会有不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,活动的导航和我想不出什么我做错了。

I'm having an issue with activity navigation and I can't figure out what I'm doing wrong.

我有一个 MainActivity SettingsActivity ,但使用返回向上(操作栏)已经从设置活动两种不同的结果。

I have a MainActivity and a SettingsActivity but using the Back and Up (on the action bar) have two different results from the Settings Activity.

例如如果我preSS返回按钮,我得到中的以下生命周期回调的 MainActivity

For example If I press the Back button I get the following lifecycle callbacks within the MainActivity:

V/lifeCycle: onOptionsItemSelected
V/lifeCycle: onPause
V/lifeCycle: onSaveInstanceState-Bundle[..]
V/lifeCycle: onStop
<< PRESS BACK BUTTON >>
V/lifeCycle: onRestart
V/lifeCycle: onStart
V/lifeCycle: onResume
V/lifeCycle: onPostResume

而如果我preSS的向上导航键,我得到这些结果:

While if I press the Navigate UP button i get these results:

V/lifeCycle: onOptionsItemSelected
V/lifeCycle: onPause
V/lifeCycle: onSaveInstanceState-Bundle[..]
V/lifeCycle: onStop
<< PRESS NAV UP >>
V/lifeCycle: onDestroy // Problem
V/lifeCycle: onCreate  // Seems
V/lifeCycle: onStart   // Here
V/lifeCycle: onResume
V/lifeCycle: onPostResume

问题是,当我preSS的导航UP 我的主要活动被破坏并重新创建这意味着我失去所有我的视图状态,但pressing背按钮不能做到这一点。

The issue is when I press the Nav UP my Main Activity gets destroyed and recreated meaning I loose all my view states, but pressing the back button does not do this.

我不知道这是否是我如何启动 preferenceActivity

I'm not sure if it's how I'm starting the PreferenceActivity:

Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;

还是我在的Andr​​oidManifest.xml 意图是如何配置的:

Or how my intents in AndroidManifest.xml are configured:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".SettingsActivity"
    android:label="@string/title_activity_settings"
    android:parentActivityName=".MainActivity">
    <intent-filter>
        <category android:name="android.intent.category.PREFERENCE" />
    </intent-filter>
</activity>

这可能导致该问题,或者这只是我需要重写,如果是这样,什么是正确的方式来重写正确的行为?

That might be causing the issue, or if this is just the correct behaviour that I need to override and if so, what is the "correct" way to override?

推荐答案

删除

安卓parentActivityName =。MainActivity

android:parentActivityName=".MainActivity"

从清单文件中的 SettingsActivity 的。

然后在你的SettingsActivity类,请执行以下操作:

Then in your SettingsActivity class, do the following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId())
    {
        case android.R.id.home:
            this.finish();
            return (true);
    }
    return super.onOptionsItemSelected(item);
}

这篇关于背部和导航向上会有不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆