0

I'm developing a scientific app in Android Studio. It works smoothy.

The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.

All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.

However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.

There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.

There is no error message, no logcat. Nothing.

Mostly, when I lock the screen and enter again, all my app data is there.

PS: My orientation is locked.

PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.

if (!isTaskRoot() {
      && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
      && getIntent().getAction() != null
      && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

      finish();
      return;
    }

No changes for me.

Update:

I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).

For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.

exit button

Paulo Buchsbaum
  • 2,471
  • 26
  • 29

1 Answers1

-1

It's my first app that I developing in mobile world and, for extension, Android world

Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!

And, in this situation, it's not the case. No hints. Just question marks.

Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.

I've stumbled in solution. Fortunately!

Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)

When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click

So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.

And this is done simply by putting in the onCreate, for my only activity, the following code.

 override fun onBackPressed() {
    val i = Intent(Intent.ACTION_MAIN)
    i.addCategory(Intent.CATEGORY_HOME)
    startActivity(i)
  }
Paulo Buchsbaum
  • 2,471
  • 26
  • 29
  • `active application stack.`. Wrong, the 'App Switcher' is not called an 'Active Application Stack'. Also, overriding the behavior of the back-button is BAD. – Zun Aug 01 '19 at 15:18
  • It's always a matter of opinion. My kind of `app` demands a specific button to a real app exit, like old times applications. I don't like rigid rules. VIsually the effect is the same of back, it exits the app normally showing the home screen. The only difference is that when one resumes the app, all data is still there. – Paulo Buchsbaum Aug 01 '19 at 15:30
  • [Zun](https://stackoverflow.com/users/3999808/zun), for me it has worked very smoothy. I've tested exhaustively in my app. One only lose app data of his session, if he really wish exit the program. Data load is not high enough to justify this kind of memory saving, that harms the `Ux`. – Paulo Buchsbaum Aug 01 '19 at 15:34
  • [Zun](https://stackoverflow.com/users/3999808/zun), you would know that Facebook app, instance, works exactly in the same way.? Any time, when you press `back` button and return to Facebook app, he resumes exactly in the same screen, precisely in the same scrolling position. I.e, they overwrite the normal `back` behaviour. – Paulo Buchsbaum Aug 01 '19 at 15:51