gpt4 book ai didi

Android 不会保存 Activity 的当前状态

转载 作者:行者123 更新时间:2023-11-29 22:30:34 25 4
gpt4 key购买 nike

我正在按照此处的示例尝试在我的 Activity 的 onSaveInstanceState(Bundle) 方法中保存一些值:Saving Android Activity state using Save Instance State

但它似乎没有从 Oncreate() 加载它。 bundle 对象始终为空,但每当我调用另一个 Activity 时,它确实会进入 onSaveInstanceState 方法以保存我的值。

现在我读了我刚刚发布的那个问题,有人指出他们怎么不能让它在模拟器中工作?不幸的是,这就是我的全部工作。在模拟器上无法在设备上测试应用程序,因为我现在没有可用的设备而且我正在与之交互的 Web 服务位于我工作机器上的本地 VM 中,目前无法远程访问。

我的问题是,保存的 bundle 真的不能在模拟器上运行吗?我还注意到当我关闭或启动新 Activity 时,该 Activity 如何调用 onPause 然后调用 onStop。当我带回相同的 Activity 时,它会直接进入 onCreate 吗?

现在根据此处的文档 http://developer.android.com/guide/topics/fundamentals.html#actlife就生命周期图显示的内容而言,这是正确的,但是如果您在 onStop() 部分的下方阅读该图,它会说它进入的下一步是 onRestart() 还是 onDestroy()?没有提到onCreate?在文档上输入?

无论如何,我的 onSaveInstanceState() 和 onCreate() 都在同一个 Activity 中:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.d(TAG, "SAVING OauthManager in onSaveInstanceState");
// TODO: if works, we need to save the my network list HashMap as well
// so we dont make a call to the platform everytime we refresh this
// screen
// savedInstanceState.putSerializable("oauthManager", mOathManager);
// Log.d(TAG, "finished saving");
// super.onSaveInstanceState(savedInstanceState);
// Log.d(TAG, "super.onSaveInstanceState(savedInstanceState)");

savedInstanceState.putString(USER_CONSUMER_ID,
mSavedUserConsumerTokenId);
savedInstanceState.putString(USER_CONSUMER_SECRET,
mSavedUserConsumerSecret);

savedInstanceState.putString(URL_REQUEST_TOKEN, mSavedRequestTokenUrl);
savedInstanceState.putString(URL_ACCESS_TOKEN, mSavedAccessTokenUrl);
savedInstanceState.putString(URL_AUTHORIZE_TOKEN,
mSavedAuthorizeTokenUrl);
super.onSaveInstanceState(savedInstanceState);
}

我的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, " ");
Log.d(TAG, "onCreate");
Log.d(TAG, "///////////////////////////////////////");
Log.d(TAG, "///////////////////////////////////////");
super.onCreate(savedInstanceState);
Log.d(TAG, "super.onCreate(savedInstanceState);");
setContentView(R.layout.network_list);
mContext = getApplicationContext();
mIntent = getIntent();


mGoogleButton = (Button) findViewById(R.id.googleAddOrRemoveButton);
mFacebookButton = (Button) findViewById(R.id.facebookAddOrRemoveButton);
mLinkedInkButton = (Button) findViewById(R.id.linkedInAddOrRemoveButton);
mPopEmailButton = (Button) findViewById(R.id.popEmailAddOrRemoveButton);


if (savedInstanceState != null) {
Log.d(TAG, "inside if (savedInstanceState != null) {");
Log.d(TAG, "savedInstanceState != null");
// mOathManager = (OAuthManager) savedInstanceState
// .getSerializable("oauthManager");
mSavedUserConsumerTokenId = savedInstanceState
.getString(USER_CONSUMER_ID);
mSavedUserConsumerSecret = savedInstanceState
.getString(USER_CONSUMER_SECRET);

mSavedRequestTokenUrl = savedInstanceState
.getString(URL_REQUEST_TOKEN);
mSavedAccessTokenUrl = savedInstanceState
.getString(URL_ACCESS_TOKEN);
mSavedAuthorizeTokenUrl = savedInstanceState
.getString(URL_AUTHORIZE_TOKEN);

mOathManager = new OAuthManager(mContext, getIntent(),
mSavedUserConsumerTokenId, mSavedUserConsumerSecret,
mSavedRequestTokenUrl, mSavedAccessTokenUrl,
mSavedAuthorizeTokenUrl, CALLBACK_URI);

mOathManager.requestUserRequestToken();
} else{

Log.d(TAG, "savedInstanceState is null and loading the list again");
GetNetworkListTask getNetworkListTask = new GetNetworkListTask();
getNetworkListTask.execute();
}




}

最佳答案

the bundle object is always null but whenever i dismiss the activty it does indeed go into the onSaveInstanceState method to save my values.

如果“关闭”是指“按下后退按钮”,那么 onSaveInstanceState() 中的 Bundle(如果有)将被丢弃,正如用户所指出的他们完成了 Activity 。 onSaveInstanceState() Bundle 用于用户没有说他们完成 Activity (例如,他们接听来电)但 Android 选择销毁的情况释放 RAM 的 Activity 。

My question is, is it true that saved bundles dont work on emulators?

onSaveInstanceState() 在模拟器中运行良好。在模拟器中测试 onSaveInstanceState() 的最简单方法是更改​​屏幕方向 ([Ctrl]-[F11])。

I also noticed how when i do dismiss or bring up a new activity , the activity calles onPause and then onStop. when i bring back the same activty, it goes straight to onCreate?

您对 Android 有一些基本的误解,远远超出了单个 StackOverflow 答案的范围。

but if you read below that diagram on the onStop() section it says the next step it goes into is either onRestart() or onDestroy()? no mention of onCreate? Type on the docs?

图表大体上是正确的。在某些情况下,onDestroy() 不会被调用(例如,紧急 RAM 回收迫使您的进程被杀死),但这种情况并不常见。

关于Android 不会保存 Activity 的当前状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4204356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com