Question

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 .?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top