2013年9月12日星期四

[Android] Activity on the state of preservation of the doubts

According to the documentation of view , only when an activity becomes "easy" onSaveInstanceState destroyed by the system will only be invoked if the user presses the back button to take the initiative to destroy an activity, onSaveInstanceState will not be invoked . .

Is there any way that the user can press the back button when the state of preservation , restart app after restore it ? Like hands , like Q , such as hand Q expand a contact list, and then press the back key to exit to the main screen , and then re- hands Q icon in, contact list or to maintain the expanded state ?

pursuing big God answering
------ Solution --------------------------------- -----------
back after, will onDestroy () , and save the state of what use it .
proposed landlord may consider using SharedPreferences to save some of the state to show the UI when , according to the different states to show a different UI.
------ Solution ---------------------------------------- ----
point back will not necessarily destroy Activity, unless your application there is only Activity, if it points back service will call onstop unless the memory system will kill you . When the program exits to return to what come back or what kind
------ Solution --------------------- -----------------------

recommend using this method to save the relevant state, so do not worry about system destroyed ;
- ---- eference ---------------------------------------
find solutions a :
1. use a Service to save state : This multi destroyed for real activity
2. moveTaskToBack (true): Capture button event using moveTaskToBack way for activity to the background , without actually destroying it

The second way to meet my needs



    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//注意这一句,它重写了用户按下back键触发的事件,这里执行moveTaskToBack只是将activity移到后台,并没有销毁
            moveTaskToBack(true);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


Thank you

没有评论:

发表评论