0

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.

Carl
  • 841
  • 1
  • 13
  • 33

1 Answers1

0

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.

Mikhail
  • 3,666
  • 4
  • 30
  • 43
  • Yes, I have debugged through. It only started hitting onSaveInstanceState() when I added the onPause() method. – Carl Mar 19 '15 at 07:49