Forum Xamarin.Android

OnSave/RestoreInstanceState by back button pressing not called but destroyed

CortezCortez USMember ✭✭✭

Hello Everyone!

I realized that i don't understand perfectly the activity lifecycle or something wrong. I have 3 actvitity. Main activity and two alternative activity which are reachable through the menu. The first menu item is Configuration and the other is the Storing. I need one configuration item to be setted(e-mail address). I am going to the Storing activity. There is a listview with a button. In the listview there are custom listview items. A checkbox with textview. I check some list items and click the button. I want to send the checked listview items (files) send to an e-mail address what i specified in the configuration activity. I checked the neccessary thing before send e-mail. Is wifi connected, i am online, e-mail address is specified, checked minimum one file and so one. If the e-mail is empty then i must navigate back to the main form with back button and then press the menu button and choose "Configuration". There i specify the e-mail address, press back button to the main activity again and hit the menu button again and navigate to the "Storing" activity. I know it is a little complicated. But i want to store the checking in the listview. I checked that the OnPause(), OnStop() and OnDestroy() called but when i want to save the checkings the OnSaveInstanceState() and OnRestoreInstanceState() don't called. I know that if the activity not destroyed it holds his status. On this layout if the wifi not connected i call an activity (ActionWifiSettings) to choose the user a connection. After that i navigate back to the Storing activity and the checkings are fine. I don't want to let the activity to destroy itself but it do it. I think if the OnCreate() call only once it would be good. But every back button the OnDestroy() is called. I don't push the home button, just the back button. I stay in the application. When i navigate to the Storing activity i want to see the previously checked files. In this application is running a service too. If i close this application and the service running it is okay, that the application forget the checked files, but if this application run in background, i don't want to let.

Best Answers

Answers

  • CheesebaronCheesebaron DKInsider, University mod

    Please... No one wants to read a wall of text to get what you ask for. Simplify your question, use pictures, diagrams, code snippets and even using line breaks help.

  • CortezCortez USMember ✭✭✭

    Ok sorry, but never can paste other sources perfectly:pictures, source codes. I dont know how to paste diagrams.

    How can i reach that if on an activity press the back button don't call the onDestory() ?

  • CortezCortez USMember ✭✭✭

    I will, but now i want to programming not reading.

    When i leave an activity with back button, then it is destroyed. Fine. But why don't call the save and restore methods? I can imagine the activity stack in this situation.

    | |

    |Main Activity|

    |Storing |

    |Main Activity|

    | |

    |Main Activity|

    |Configuration|

    |Main Activity|

    | |

    |Main Activity|

    |Storing |

    |Main Activity|

    In the Configuration activity the settings are saved into the Configuration file. In the OnResume() method i load this configuration and in the OnPause() method is save it. Should i use this method in the Storing activity too? Save the checked files? But the save and restore methods didn't call.

  • CheesebaronCheesebaron DKInsider, University mod

    If you cannot make yourself understandable, it is difficult to help you with your problem.

  • CortezCortez USMember ✭✭✭

    I know but i asked simple questions not a wall of text as you said. I expected a very simple situation but i wasn't. When i hit the back button the current activity destroys. Fine. But why not firied the OnSaveInstanceState() method to save the status?

  • CheesebaronCheesebaron DKInsider, University mod

    The discussion is going off topic. But to me 10 lines of text with no line breaks, loosely trying explain the entire problem does not make sense to me and does not want me to read the question. It is not simple at all. You didn't even have a question mark in your first post!

    Anyways. OnSaveInstanceState() does not called immediately. It gets called when at some point Android does not want the Activity to idle in the memory anymore, or when it wants to re-create the Activity. However, OnPause() gets called when the Activity is not visible anymore.

    Android Activity Lifecycle

    The diagram explains it pretty well. The table on this page: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle where stuff is "Killable", when the Activity is in such a state OnSaveInstanceState() is called.

  • CortezCortez USMember ✭✭✭

    I supposed that this is working as you explained. I set breakpoints in the activity in OnPause(), OnStop() and OnDestroy().
    Every breakpoints hit.
    After that i implemented the OnSaveInstanceState() method and set a breakpoint in it too. This breakpoint never hit.
    In an another activity working the same, but there i save the configuration manually into my configuration file in the OnPause() method and load it in OnResume() method. The other methods(OnStop(), OnDestroy()) called to, but i use my own saving. But this OnSaveInstanceState() and OnRestoreInstanceState() is for this situation. If i starting a new activity (navigate to the wifi settings page) then this method called.

    In this case the OnPause() method called and the OnSaveInstanceState() too, the OnStop() and OnDestroy() shouldn't. This behavior is opposite what i expected. If this actvitity go into the background, not need to save its status because the instance not will be null.

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    Also, be aware that OnSaveInstanceState isn't always called. Sounds like the situation you're describing. To quote the Android docs (I've bolded the relevant line for your scenario @Cortez) :

    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. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. 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.

  • CortezCortez USMember ✭✭✭

    @topgenorth

    Good to know. Important part of understand the movement. If i understand what the doc said. The system think it is not sure that the user navigate back to this activity and restore the saved data. Then why should i store it, i don't call the OnSaveInstanceState() method.

    I start the activity (Config) if the user not define yet the e-mail address. If the user setted everything important on the Config layout then with the back button can navigate to the Storing layout. In this case the Storing activity push in the activity stack (above the Main activity), and when i navigate back to this activity(Pop the Configuration activity) the settings not change.

    However i want to store the previous checkings. If i have 30 log files and the user checks 15, i want to store for him those. Should i use SharedPreferences or saving the checkings in another file? Because this OnSaveInstanceState is not reliable.

  • CortezCortez USMember ✭✭✭

    Ok i will use this. Thank you for help @Tomasz and @Tom.

Sign In or Register to comment.