-1

Does anyone know of a way to show another class without creating a new instance? It seems a bit crazy from a memory management point of view that each time you want to display a different form / page you need to use StartActivity which then creates a new instances of the class instead of reusing instances previously created.

Thanks in advance

I guess from what has been said - there is no real way to do it which won't hinder the "Back" functionality of the OS? I'm building an app which is linear except on each screen it has a home button which then makes it possible to countermand this functionality and end in a loop - is there anyway you know of to destroy all over views and reset back to the main class? (IE prevent a memory leak from becoming a problem but also not damaging OS functionality)

Consider it a "clear history" without restarting the app

TheBritishAreComing
  • 1,667
  • 2
  • 19
  • 38

2 Answers2

1

Not sure if this would work for Android (coming from a MS/C# background), but conceptually one option is to iterate through open forms looking for one with a specific handle. Then, once you find it, simply call the method to show that form. This would depend on there being a Java equivalent to the Application.OpenForms property in .NET.

A. Wilson
  • 688
  • 1
  • 6
  • 15
0

It seems a bit crazy from a memory management point of view that each time you want to display a different form / page you need to use StartActivity which then creates a new instances of the class instead of reusing instances previously created.

Tactically, you are welcome to add FLAG_REORDER_TO_FRONT to bring an existing activity back to the foreground, so long as you understand the ramifications from a navigation standpoint.

However, your question is rather curious. How are you accessing StackOverflow?

Clearly it's not via a Web browser. Web browsers use the exact mechanism that you feel is "crazy", rendering Web pages even if that Web page had been viewed previously. They have been doing so for over 15 years, and we've been doing OK by it.

The Android navigation model is designed to approximately mirror that of the Web:

  • Users click on things to move forward
  • Users click on a BACK button to move to the previous thing they were looking at
  • Users click on a HOME button when they want to switch to some other major thing to go look at

By "reusing instances previously created", you're circumventing that navigational model. For example, let's suppose your activity stack were A-B-C-D, and you call startActivity() with an Intent for B and FLAG_REORDER_TO_FRONT. Now, the activity stack is A-C-D-B. When the user presses BACK times, they no longer are on the B they were looking at originally, but are back at A. In a browser, this would be rather strange behavior.

There are other flags on Intent, or attributes on <activity> in the manifest, that offer "reusing instances previously created". However, they are not there "from a memory management point of view". They are there where the traditional Web BACK-heavy navigation pattern does not fit your needs.

Assuming you aren't screwing up anywhere, Android will destroy under-utilized activities, garbage collect that memory, and even return that memory to the OS.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for your explanation, Supposed I have a model A-B-C-D and on D I have a button which displays A then in the memory we have A-B-C-D-A and then I follow the menu system from A I could end up having A-B-C-D-A-B-C-D-A-B-C-D and so on, android doesn't seem to remove the under utilized states otherwise I wouldn't be able to click the back button and return to state A-B-C-D hence the question - The browser analogy is slightly different in that the Browser doesn't store the website in memory, it re-parses the page with the exception of using the file based cache. – TheBritishAreComing Apr 22 '11 at 18:56
  • I guess from what has been said - there is no real way to do it which won't hinder the "Back" functionality of the OS? I'm building an app which is linear except on each screen it has a home button which then makes it possible to countermand this functionality and end in a loop - is there anyway you know of to destroy all over views and reset back to the main class? (IE prevent a memory leak from becoming a problem but also not damaging OS functionality) Consider it a "clear history" without restarting the app – TheBritishAreComing Apr 22 '11 at 19:11