I have an activity which is send some data to the server at onCreate.My problem is that if I press on lock button on the phone the screen turning off as we all knew but when I press the lock button to turn on the button the screen will turn on and it sending the data again so it is replicating the data. I just need to send the data only one time.what to do .?

有帮助吗?

解决方案

Send the data in onCreate but only if savedInstanceState is null:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    if(savedInstanceState == null) {
        // send data in a background thread
    }
}

If the activity is re-created (your case), the savedInstanceState is not null and it will contain any statefull info you would need to set.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top