android:保存活动状态
我知道这个问题已经被问过很多次了,但我尝试过很多解决方案,但都没有用。呜咽呜咽 我会尽量保持简单。我正在写一个应用程序,它使用微调器和编辑文本来输入浮点数。因此,屏幕A有4个微调器和4个编辑文本。我使用“下一步”按钮打开屏幕Bandroid:保存活动状态,android,Android,我知道这个问题已经被问过很多次了,但我尝试过很多解决方案,但都没有用。呜咽呜咽 我会尽量保持简单。我正在写一个应用程序,它使用微调器和编辑文本来输入浮点数。因此,屏幕A有4个微调器和4个编辑文本。我使用“下一步”按钮打开屏幕B onClick(){ a.putExtras(sendScoreSoFar); a.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(a); } 屏幕B与3个微调器和2个
onClick(){
a.putExtras(sendScoreSoFar);
a.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(a);
}
屏幕B与3个微调器和2个编辑文本大致相同。
我可以从A移到B再移回A,同时保留用户的输入,但当我再次返回B时,B被重置。我需要保留输入,以便用户可以来回切换以检查输入。我可以使用共享首选项,但需要在第一次加载时重置
我试过清单上的几行字:
<activity
android:name="com.package.Screen2"
android:label="@string/app_name"
android:alwaysRetainTaskState="True"
android:launchMode="singleInstance">
>
<intent-filter>...
我知道有一个简单的解决办法,但我找不到。
提前谢谢
编辑
据我所知,您不能将数据发送到onResume
因此,尽管给出的答案是正确的,但我通过删除所有共享首选项代码,删除对清单的更改,解决了问题:
android:alwaysRetainTaskState=“True”
android:launchMode=“singleInstance”>
而不是试图保留该活动,而是像正常情况一样终止它(使用“后退”按钮)
只需发送微调器的值并使用startactivityforresult编辑捆绑包中的文本。
screenA启动screenB发送bundle a,然后当screenB用back按钮终止时,它将bundle b发送回screenA,然后screenA现在启动screenB时,它发送bundle a和b,b用bundle b中的值填充。
可能没人感兴趣,但我想我还是提出了解决方案。
如果有人愿意,我可以提供代码。谢谢你的回答,伙计们,我下次会查看你的选项。我假设你正在通过“后退”按钮返回到一个屏幕,该按钮将完成你的B屏幕
您可能想考虑将每个屏幕放入一个片段中,将它们加载到您的活动中,并使用片段管理器在片段之间翻转。 以下是与此解决方案相关的资源
您可以使用片段生命周期回调来处理数据,并使用setRetainInstance()来“控制片段实例是否在活动重新创建过程中保留(例如从配置更改中保留)”如果您需要预蜂窝兼容性,请查看支持库以执行此操作。您可以选择保留数据(例如,使用您已经知道的SharedReferences)。或者,您可以创建一个保存您的值的服务。这两个活动都绑定到服务,并且可以从服务获取和设置值。该服务将在整个活动转换期间保持活动状态,因此ActivityA设置的值将可用于ActivityB
保存活动状态只会将状态持久化为相同的活动类型。它用于恢复活动或持续状态,例如方向改变。什么是呜咽?呜咽-为某事悲伤伟大的解决方案。我会选择服务,但这是一种更有效的方式+1.通过截取后退按钮并使用它启动活动a,以及清单中的android:alwaysRetainTaskState=“True”android:launchMode=“singleInstance”,我几乎可以让它正常工作。问题是活动A上的后退按钮不再退出应用程序。有什么建议吗?我可能不得不求助于碎片解决方案
@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"));
}