- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在做一个关于 eclipse android 开发的教程系列,我正在尝试复制代码以帮助记住所有功能,但我收到 onSaveInstanceState(Bundle) 的错误,它说“onSaveInstanceState(Bundle) 方法是对象类型未定义”。我已经多次检查以确保一切准确无误,但没有发现任何错误。
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putDouble(TOTAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(BILL_WITHOUT_TIP, finalBill);
}
最佳答案
您需要使用@Override
并确保您的类扩展Activity
。只要具备这两个条件,您就应该做得很好。
class MyActivity extends Activity {
@Override // May be public depending on the class you are extending
protected void onSaveInstanceState(Bundle outState) {
outState.putDouble(TOTAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(BILL_WITHOUT_TIP, finalBill);
// Wait till after you've added your items to pass the bundle
super.onSaveInstanceState(outState);
}
}
根据Activity文档页面其他扩展 Activity
的类是 AccountAuthenticatorActivity , ActivityGroup , AliasActivity , ExpandableListActivity , FragmentActivity , ListActivity , NativeActivity , ActionBarActivity , LauncherActivity , PreferenceActivity和 TabActivity .
如果您想弄清楚某些东西是如何工作的,那么 Android 源代码也是一个不错的地方。很多时候,这些注释更多地描述了事情是如何实现的以及它们依赖什么。
引用:core/java/android/app/Activity.java
/**
* Called to retrieve per-instance state from an activity before being killed
* so that the state can be restored in {@link #onCreate} or
* {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
* will be passed to both).
*
* <p>This method is called before an activity may be killed so that when it
* comes back some time in the future it can restore its state. For example,
* if activity B is launched in front of activity A, and at some point activity
* A is killed to reclaim resources, activity A will have a chance to save the
* current state of its user interface via this method so that when the user
* returns to activity A, the state of the user interface can be restored
* via {@link #onCreate} or {@link #onRestoreInstanceState}.
*
* <p>Do not confuse this method with activity lifecycle callbacks such as
* {@link #onPause}, which is always called when an activity is being placed
* in the background or on its way to destruction, or {@link #onStop} which
* is called before destruction. One example of when {@link #onPause} and
* {@link #onStop} is called and not this method is when a user navigates back
* from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
* on B because that particular instance will never be restored, so the
* system avoids calling it. An example when {@link #onPause} is called and
* not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
* the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
* killed during the lifetime of B since the state of the user interface of
* A will stay intact.
*
* <p>The default implementation takes care of most of the UI per-instance
* state for you by calling {@link android.view.View#onSaveInstanceState()} on each
* view in the hierarchy that has an id, and by saving the id of the currently
* focused view (all of which is restored by the default implementation of
* {@link #onRestoreInstanceState}). If you override this method to save additional
* information not captured by each individual view, you will likely want to
* call through to the default implementation, otherwise be prepared to save
* all of the state of each view yourself.
*
* <p>If called, this method will occur before {@link #onStop}. There are
* no guarantees about whether it will occur before or after {@link #onPause}.
*
* @param outState Bundle in which to place your saved state.
*
* @see #onCreate
* @see #onRestoreInstanceState
* @see #onPause
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
getApplication().dispatchActivitySaveInstanceState(this, outState);
}
关于android - onSaveInstanceState(Bundle) 方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19342953/
我是 Android 开发新手。我正在使用 onSaveInstanceState() 的覆盖版本来保存我自己的应用程序数据。我注意到我没有在我的函数中调用 super.onSaveInstanceS
如果我将在不调用 super.onSaveInstanceState(outState) 的情况下使用此代码; @Override protected void onSaveInstanceState
我在 fragment 兼容性包示例之后创建了一个 fragment 选项卡小部件,我在 onTabChanged 中更改了 fragment ,但我有一些市场报告在 onSaveInstanceSt
当我离开我的 Activity 时,为什么我的应用程序崩溃了?更具体地说:在我实现 onSaveInstanceState() 之前一切正常和 onRestoreInstanceState() 这是代
我有一个 Activity ,它启动结果代码的各种 Activity ,并在 onActivityResult 方法中获取结果时,它根据结果代码启动适当的 Activity 。 onSaveInsta
我在 onSaveInstanceState 上保存当前进度条时遇到问题。我保存当前正在进行的时间,但是当退出 Activity 并返回时恢复进度不显示剩余时间。 该类显示进度条 public cla
我想实现什么目标? 我想保存 x、y 和 score 的值,因此当用户旋转屏幕时,会检索这些值,而不是重获新生。 我有什么? 可能是正确的 onSaveInstanceState 代码,但由于某些 @
所以我了解了如何使用的主要想法 protected void onSaveInstanceState (Bundle outState) http://developer.android.com/
考虑以下代码,它将静态变量:mMyArray 保存在 onSaveInstanceState 中,并在 onRestoreInstanceState 中恢复它,但是我发现它根本无法保存 int 数组。
当我的应用程序变得复杂并且主要 Activity 开始在内存中保存大量数据(响应用户操作从文件加载和解析)时,我(错误地)从 onSaveInstanceState 切换到 onRetainNonCo
我是我应用的 onSaveInstanceState 实现。我已经成功实现了我的 onSaveInstanceState,但它不会在方向更改时保持当前问题。以下是我的 MainActivity。我一直
我正在尝试在单击后退按钮时保存和检索我的 Activity 的 onSaveInstanceState()。我读过默认情况下它不会保存,因为假设用户在单击返回时已完成 Activity 。但是,在我的
我已经阅读了android文档中的以下声明 Because onSaveInstanceState() is not guaranteed to be called, you should use i
我有几个扩展 LinearLayout 的自定义组件。 现在我想在屏幕方向改变时保存/恢复它们的状态,但我不能通过 Activity 的 onSaveInstanceState/onRestoreIn
我正在尝试了解生命周期方法和 onSaveInstanceState()。 我实际上只需要担心 onCreate() 和 onPause() 是否正确? onCreate()? 我可以/应该忽略 on
我是 Android 开发的新手,我有一个关于 onSaveInstanceState() 的问题。我目前正在处理一个应用程序的登录 Activity 。为了检查用户是否可以登录他们的帐户,我对服务器
我正在编写一个程序,我在同一个 Activity 中调用多个布局,但后来我注意到当我切换布局时,切换之前所做的更改不会恢复并且 onSavedInstanceState(Bundle outState
当 android 由于系统限制而杀死您的进程时,它使您能够通过将数据存储在一个包中来跨进程持久保存数据。如果您的进程被终止,这个包保存在哪里?它存在于哪个进程中?它住在内存中的什么地方?它存在于内核
我在我的 android Activity 中实现了 onSaveInstanceState 和 onRestoreInstanceState。 这些似乎工作正常,但我想在某些事情发生时(例如,当用户
我的问题似乎与其他人相反。 :) 每当我从一个 Activity 导航到下一个 Activity 时,都会调用我的 onSaveInstanceState。我检查了 LogCat,它绝对不会终止 Ac
我是一名优秀的程序员,十分优秀!