0

I experience a very strange problem.

From the MainActivity of my app, by clicking on one of my menu button, a new screen / fragment called MyFragment is loaded. On the MyFragment screen there is a button called MyButton. If the MyButton button is clicked, it opens a link on a browser. The problem is that, from the browser, if I click on the Back device button, the behavior is very different depending on the device I test the app (and you will all agree it should NOT) :

  • on a Samsung Galaxy Tab 2 tablet, the app gets back to its previous state, with the MyFragment loaded and opened (just as if nothing changed from the moment when I clicked on MyButton).

  • on a Nexus 4 phone, the app is like reset (and then the MyFragment is not loaded). Basically the previous state is lost.

Any idea ?

Here is the piece of code I call from the MyButton click event.

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.example.com"));
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // I tried this but no luck
startActivity(i);

Thanks in advance.

thomaus
  • 6,170
  • 8
  • 45
  • 63

2 Answers2

1

It sounds like on some phones your Activity is geting killed by the OS. Android has the right to kill any Activity that goes in the background if it needs to reclaim the resources that it's using. This is the expected behavior. To mitigate this, you'll need to manage the Activity Lifecycle. The important thing to note from that page are that onSaveInstanceState() will allow you to save your state if the OS is about to kill your Activity, which will restart the lifecycle with onCreate() and provide you with a Bundle that contains all your saved state information.

Additionally, fragments have their own lifecycle, with the equivalent onSaveInstanceState() method, so any Fragment-specific info should be managed there.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
  • Don't you think there is a way to avoid that the apps gets killed? – thomaus May 27 '13 at 14:04
  • 1
    No, the first reference from my post in the "Saving Activity State" section explains that this is the way the operating system was designed. The OS has the right to kill a background activity or application to reclaim memory at any time. It's up the to us developers to handle that situation smoothly. The purpose of the "Don't keep activities" checkbox that you mention in your answer is so that the application can be tested under these conditions. Certain fields, such as EditTexts, may persist, but any serious state needs to be managed with onSaveInstanceState(). – MattDavis May 28 '13 at 02:29
0

Answer : on the Nexus 4 "Developer options", the "Don't keep activities" option was set to true. That's why...

thomaus
  • 6,170
  • 8
  • 45
  • 63
  • This is helpfull feature to "oblige android" to behave in the way it will sometimes do as explained by @MattDavis. You HAVE to consider this case, and thus it's great to be able to simulate it with this option. – Snicolas Jun 21 '13 at 10:29