-1

I have an activity and the activity does some code behind when the screen orientation changes. So if the users decides to flip the screen back and forth the code behind will break. I want to catch that with a try/catch and in the catch i want the activity to restart or maybe if it is easier to go back one step to the activity before it. Im successfully catching the exception right now but i don't really know what to do in the catch.

If tried this:

  OnDestroy();
  StartActivity(typeof(ImageGridActivity));

or simply

 Finish();

so that it hopefully just jumps back one activity. but it doesn't. Plz help!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • do you want to change the screen orientation when the screen is rotated or you want always to use one orientation? If you choose the second approach, your activity will not be restarted and you will just need to do some changes in the manifest file. Look at http://developer.android.com/guide/topics/resources/runtime-changes.html – dragi Aug 15 '14 at 11:38
  • i want it to be able to change orientation but if the user does that a couple of times to fast the code behind wont be able to finish so it will break. In that case i want it to finish the activity and return to the activity before or just restart the activity from the begining. I just dont want it to break like it does now. – Daniel Gustafsson Aug 15 '14 at 11:41

1 Answers1

0

When the screen orientation changes, your old activity goes (out of screen), and a new one starts. If the old activity is not referenced, it will be garbage-collected, otherwise it will still occupy memory. The onCreate() method has a Bundle parameter; when it's not null, it means a restart of the activity. See onSaveInstanceState().

If you do something in background, the task must not be owned by the activity (which is a Controller from the MVC viewpoint), it should belong to Model. You can either store a reference in a global variable or do as suggested in onRetainNonConfigurationInstance() (there's much to read).

See also: Is AsyncTask really conceptually flawed or am I just missing something?

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127