Android app restarts if it gets out of focus for a while

Asked

Viewed 185 times

1

My app is an intermediary for a service request, e.g.: (UBER). What happens is that the main screen of the business, which is the one that the professional is moving to go find the customer, disappears after a while if the user is using other apps, for example make a call outside the app. I want to know how to keep this screen until this request is completed?

  • That’s normal, that’s how the Android works. The solution depends on each case. Read what the documentation says on the subject.

  • Hello @ramaral, I edited the question to improve the explanation. I will take a look at the link, thanks.

  • Disappears completely or is the process destroyed? It does not appear in "Recent applications"?

  • It continues to appear in recent applications, only when I click it goes back to Mainactivity.

  • @The normal is for him to go back to the same Activity he was, but restarting it. If you want to know if this is the effect of the system killing Activity due to lack of resources, turn on the "Do not keep activities" option in the developer settings. Open your program, switch to another and go back to it. If the same effect happens, your problem is the same.

2 answers

4


Before you start developing for android you should know the life cycle of an activity (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 that link. Note: is in English.

  • But that’s if Activity calls onDestroy(), correct?

  • 1

    One fix: onCreate will only run again if Activity is destroyed within this range.

  • @The Activity can be destroyed without the method onDestroy() be called.

  • Thanks, I managed to solve it that way :)

0

For your application to keep data in the background, the best way I know is to use Service or Intentservice, which initiate Threads that will remain running even after the android 'kill' the Activity q has the layout. To keep your data, 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: http://developer.android.com/intl/pt-br/guide/components/services.html

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

  • I cited the two classes as possible solutions, the link is only to have a follow-up of where to look, which will be done even if the link is broken, as it is from the 'Developer.android.com'... the essential part of the answer is in the text, is an indication, since the question is not about the code itself, so I did not see the need to include any specific code.

  • 1

    Anyway, this is "Overkill". What AP needs is just that the data is restored when reopening the screen. This can be done only with standard Activity Click Life methods.

  • @Diegof, I have no idea why this came to Low Quality Publications... Has enough explanation not to be considered "Only a link", your vote to delete is not justified.

  • @brasofilo the other answer presents and exemplifies how to save the state of an Activity, my vote was based on this, in addition to what was cited by Pablo also, nor need Services for this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.