简体   繁体   English

Android - 使用 onSaveInstanceState null 恢复活动

[英]Android - restoring activity with onSaveInstanceState null

I am having trouble restoring the state of one of my activities.我无法恢复我的一项活动的状态。 I am starting activity B from within activity A with我从活动 A 中开始活动 B

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            // Display dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(TransactionActivity.this);
            builder.setTitle(null)
                    .setItems(R.array.tran_options_array, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // The 'which' argument contains the index position
                            // of the selected item
                            switch (which) {
                                case 0:
                                    //
                                    // View
                                    //
                                    // Load transaction detail activity
                                    Intent intent = new Intent(getApplicationContext(),
                                            TransactionDetailActivity.class);
                                    Bundle bundle = new Bundle();
                                    Tran transaction = mTransactionList.get(position);
                                    bundle.putSerializable("transaction_key", mTransactionList.get(position));
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                    break;...

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("type", mType);
    super.onSaveInstanceState(outState);
}

After startActivity(intent) onPause() is called and then onSaveInstanceState().在调用 startActivity(intent) onPause() 和 onSaveInstanceState() 之后。 Clicking on the back button on activity B then results in onDestroy() being called in Activity A and then onCreate() with the (Bundle savedInstanceState) as null.单击活动 B 上的后退按钮会导致在活动 A 中调用 onDestroy(),然后在 (Bundle savedInstanceState) 为 null 的情况下调用 onCreate()。

Are you sure that onSaveInstanceState() is called?您确定调用了 onSaveInstanceState() 吗?

As from the documentation for the onSaveInstanceState() :根据onSaveInstanceState()文档

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction.不要将此方法与活动生命周期回调混淆,例如 onPause(),它总是在 Activity 被放置在后台或正在销毁的途中被调用,或者 onStop() 在销毁之前被调用。 [...] An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) 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. [...] 调用 onPause() 而不是 onSaveInstanceState(Bundle) 的一个例子是当活动 B 在活动 A 之前启动时:如果活动 A 没有被杀死,系统可能会避免在活动 A 上调用 onSaveInstanceState(Bundle) B 的生命周期,因为 A 的用户界面状态将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM