Android - 使用onSaveInstanceState恢复活动null

时间:2015-03-18 21:45:28

标签: android android-activity

我无法恢复其中一项活动的状态。我从活动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);
}

在startActivity(intent)之后调用onPause()然后调用onSaveInstanceState()。单击活动B上的后退按钮,然后在活动A中调用onDestroy(),然后使用(Bundle savedInstanceState)调用onCreate()为null。

1 个答案:

答案 0 :(得分:0)

你确定调用了onSaveInstanceState()吗?

来自documentation for the onSaveInstanceState()

  

不要将此方法与活动生命周期回调混淆,例如onPause(),当活动被放置在后台或去往销毁的路径时,或者在销毁之前调用的onStop()时,它会被调用。 [...]调用onPause()而不是onSaveInstanceState(Bundle)时的示例是在活动A前面启动活动B时:如果活动A不是,则系统可以避免调用onSaveInstanceState(Bundle)因为A的用户界面的状态将保持不变,所以在B的生命期内被杀死。