android:保存活动状态

时间:2013-04-25 13:49:35

标签: android

我知道这个问题已被多次询问,但我尝试了许多解决方案无济于事。呜咽。

我试着保持简单。我正在编写一个使用微调器和编辑文本来吸收浮点数的应用程序。所以屏幕A有4个微调器和4个编辑文本。我用下一个按钮打开屏幕B

onClick(){
    a.putExtras(sendScoreSoFar);
    a.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(a);
}

屏幕B与3个微调器和2个edittexts大致相同。 我可以从A移动到B并返回到A,同时保留用户的输入,但是当我再次返回B时B重置。我需要保留输入,以便用户可以来回检查输入。我可以使用共享首选项,但需要在第一次加载时重置。

我已经尝试过清单中的各种行:

 <activity
        android:name="com.package.Screen2"
        android:label="@string/app_name" 
        android:alwaysRetainTaskState="True"
        android:launchMode="singleInstance">
        >
        <intent-filter>...

并继续我的创建和保存实例状态代码。

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    setContentView(R.layout.screen2);
    super.onCreate(savedInstanceState);
    setUpVariables();
    if (savedInstanceState != null) {
        int a = savedInstanceState.getInt("weightlostorprev");
        spinner1.setSelection(a);
        int b = savedInstanceState.getInt("metricorimp");
        spinner1.setSelection(b);
        int c = savedInstanceState.getInt("acute");
        spinner1.setSelection(c);

        etBigWeight.setText(savedInstanceState.getString("bigweight"));
        etSmallWeight.setText(savedInstanceState.getString("smallweight"));
    }
    gotBasket = getIntent().getExtras();
    passedResults = gotBasket.getFloatArray("the data");
    passedWeight = passedResults[5];
    Toast c1 = Toast.makeText(Screen2.this, "on create run!  new run = "
            + newRun, Toast.LENGTH_LONG);
    c1.show();
    // question.setText(gotBread);
}

//@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    int a = spinner1.getSelectedItemPosition();
    savedInstanceState.putInt("weightlostorprev", a);
    int b = spinner2.getSelectedItemPosition();
    savedInstanceState.putInt("metricorimp", b);
    int c = spinner1.getSelectedItemPosition();
    savedInstanceState.putInt("acute", c);

    savedInstanceState.putString("bigweight", etBigWeight.getText()
            .toString());
    savedInstanceState.putString("bigweight", etBigWeight.getText()
            .toString());

}

//@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    int a = savedInstanceState.getInt("weightlostorprev");
    spinner1.setSelection(a);
    int b = savedInstanceState.getInt("metricorimp");
    spinner1.setSelection(b);
    int c = savedInstanceState.getInt("acute");
    spinner1.setSelection(c);

    etBigWeight.setText(savedInstanceState.getString("bigweight"));
    etSmallWeight.setText(savedInstanceState.getString("smallweight"));
}

我知道这是一个简单的解决方案,但我无法找到它。 提前谢谢。

EDIT 据我所知,你无法将数据发送到onResume 所以尽管给出的答案是正确的,我通过删除所有共享首选项代码解决了问题,删除了对清单的更改:     机器人:alwaysRetainTaskState = “真”     机器人:launchMode = “singleInstance” &GT; 而不是试图保持活动,正常杀死它(使用后退按钮) 然后使用startactivityforresult发送微调器的值并编辑捆绑中的文本。 因此,screenA启动screenB发送bundle a,然后当screenB被后退按钮杀死时,它将bundle b发送回screenA,然后当screenA现在启动screenB时,它发送束a和b,b填充b束中的值。 可能没有人感兴趣,但我认为id无论如何都提出了解决方案。 如果有人想要,我可以提出代码 谢谢你的回答,下次我们会看看你的选择。

2 个答案:

答案 0 :(得分:1)

我假设您正在通过后退按钮移回A屏幕,后退按钮正在完成B屏幕。

您可能需要考虑将每个屏幕放入一个片段中,将它们加载到您的活动中,并使用片段管理器在片段之间进行翻转。

以下是与此解决方案相关的资源。

Switching between Fragment view

http://www.vogella.com/articles/AndroidFragments/article.html#fragmentspersistence

您可以使用片段生命周期回调来处理数据,并使用setRetainInstance()来“控制是否在重新创建活动时保留片段实例(例如来自配置更改)。”

如果您需要预蜂窝兼容性,请查看支持库。

答案 1 :(得分:0)

您的选择是持久保存数据(例如,使用您已经知道的SharedPreferences)。或者,您可以创建一个保存您的值的服务。两个活动都绑定到服务,并且可以从服务获取和设置值。该服务将在您的活动转换中保持活跃状态​​,因此ActivityA设置的值将可用于ActivityB。

保存活动状态只会将状态保持为相同的活动类型。它在恢复活动或持久状态时使用,例如,方向更改。