Android application restarts if out of focus for a while [duplicate]

1

My app is an intermediary of a service request, eg: (UBER). What happens is that the main screen of the business, which is the one that the professional is moving to go find the client, disappears after a time if the user is using other applications, for example making a connection outside the app. I want to know how to keep this screen until this request is completely terminated?

    
asked by anonymous 30.03.2016 / 15:40

2 answers

4

Before you start developing for android you should know the life cycle of an activity. When the activity is no longer visible to the user the application is stopped and when the user returns to the app the oncreate will run again.

When this happens we lose the values of the variables, to avoid this we must save the values to recover later.

There are some events like:

//Armazenar valores quando a aplicação é pausada ou parada
@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("idade", 16);
}

//Recuperar valores armazenados 
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
}

Take a look at on this link . Note: it is in English.

    
30.03.2016 / 20:45
0

For your application to keep data in the background, the best way I know it is to use Service or IntentService, which initiates Threads that will remain running even after the android 'kill' Activity q has the layout. In order for your data to be kept, you could save an array / object / variable in a Service with the data you want to save, and then, when your Activity is restarted, you reload the information! See here: link

It's good to note that Services need to be used with caution, preventing them from running unnecessarily.

    
30.03.2016 / 20:26