1

In my activity B I have option "Help" which opens URL in web browser. When returning from web browser (with back key) activity is recreated. Why is this happening and how to prevent this?

EDIT: This is how i call web browser:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
startActivity(browserIntent);

When returning from browser onCreate() is called;

My logical operations: When starting app, activity A reads settings and write it to activity/class C. After that I start activty B and finish() activity A. In activity B, onCreate() method is reading some settings from activity C.

mgulan
  • 795
  • 1
  • 14
  • 33
  • Exactly what do you mean with "recreated"? Do you get a onDestroy() in your activity when starting the web browser? – mach Mar 10 '14 at 12:02
  • is recreating (onCreate) or resuming (onResume)?. Where are you doing your logical operations when you create the activity? which method? More information about how you create activity and how you call web broser could be great. – Neonamu Mar 10 '14 at 12:04

2 Answers2

1

To do that you must finish your Activity before starting browser.

Change your code to:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
finish(); // should be called from your current activity
startActivity(browserIntent);

Android doesn't store all information from your current Activity when it goes to other (i.e. to WebBrowser), so activity must be recreated to show it again.

If you still need this Activity after coming back from WebBrowser there is no way to prevent Android from recreating it. You should save all you need overriding onSaveInstanceState and recreate your Activity using savedInstanceState.

Look at Activity lifecycle. When Android need to free some memory for others processes it may kill your app (which is in background). There are also other possible paths back to your Activity running state which doesn't recreate it. (onPause -> onResume and onStop -> onRestart -> onStart -> onResume)

Ari
  • 3,101
  • 2
  • 27
  • 49
  • I have a problem here. 1. If I include finish() in my code, when returning from a browser I return to android main screen instead in my app... 2. I have tried with onSaveInstanceState and in this case it works but in some cases still not working. For example, when app is "minimized" and I click on notification in notification bar to start activity B. – mgulan Mar 10 '14 at 15:48
  • @mgulan If you need your activity when you are back from browser you must handle recreation situation. There is no other way. Check out lifecycle link I provided. – Ari Mar 10 '14 at 17:52
  • 1
    I succeeded with savedInstanceState + android:launchMode="singleTask" for every activity in app... Looks like activity is killed because of lack of memory. Thank you for your help. – mgulan Mar 10 '14 at 18:26
0

It can be because of developer option (Background Process Limit) turned ON in device settings. Or system killed your activity. On my opinion You should not rely on most common case of lifecycle. Your activity can be destroyed in case of low memory. So you should save activity's instance state as Ari said.

Shamm
  • 1,004
  • 7
  • 9