简体   繁体   中英

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

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(). 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.

Are you sure that onSaveInstanceState() is called?

As from the documentation for the 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. [...] 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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