-1

When my app starts, there is a start button. The user can start a fragment which setup time can't be neglected. When the user presses back on this fragment, his intention is probalby to leave the fragment, so it's wise to free the resources. However when he presses home (hardware home), his intention may be checking something and coming back. In this case I'd like to keep the data (not serialisable). How can I differentiate between the two cases?

Miklos Jakab
  • 2,024
  • 1
  • 23
  • 31
  • possible duplicate http://stackoverflow.com/questions/18545014/differentiate-the-functionality-of-home-button-and-back-button-in-android-activi – kalyan pvs Aug 13 '14 at 11:06
  • @kalyan pvs: That question relates to activity stack, this question is about one fragment. – Miklos Jakab Aug 13 '14 at 11:29

1 Answers1

1

While I was writing the question, I found a solution, though not the most beautiful (a flag needs to be kept). For both calls, the onPause(), onStop(), onDestroy(), onDetach() is called in both cases. The getActivity().isChangingConfigurations() returns false in both cases. But the onSaveInstanceState() is only called when the device is rotated and when the home is pressed. So:

  • Back button

    isChangingConfigurations(): false
    onPause()
    onStop()
    onDestroy()
    onDetach()
    
  • Home button

    isChangingConfigurations(): false
    onPause()
    onSaveInstanceState()
    onStop()
    onDestroy()
    onDetach()
    
  • Rotate device

    isChangingConfigurations(): true
    onPause()
    onSaveInstanceState()
    onStop()
    onDestroy()
    onDetach()
    

Note: the fragment is not retained. Retaining it would alter the whole process.

Note 2: According to documentation the onSaveInstaneState() could be called anytime before onDestroy() (it may come after onStop()).

Miklos Jakab
  • 2,024
  • 1
  • 23
  • 31