Android Question [Solved] force sub-activity to unload and reload as new, not to resume

Pflichtfeld

Active Member
Licensed User
I have an activity2, loaded from activity1. In firsttime-load of A2 I handle some stuff. In A2 I work, setting some textbox-values and so on. If I do activity2.finish and reload it, the firsttime is not recognised. It seems, that activity2 stays in memory and just resumes.
Is there a way, to get a 'virgin' Activity2 each time I load it?
 

Pflichtfeld

Active Member
Licensed User
No, I load the layout every time new, but the textboxes (f.i.) are filled with modulglobal values (user-inputs), which are not destroyed/resetted, when the activity is reloaded.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If I do activity2.finish and reload it, the firsttime is not recognised. It seems, that activity2 stays in memory and just resumes.
That's how Android works.
FirstTime is True only when you launche the program and you call any Activity the 'first time'!
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
with modulglobal values (user-inputs)
Speaking about module is a bit confusing in this context. A class variable only exists during the life time of that one class and that a module variable exists during the life time of a program and can be mutated by all classes? So where you define a global makes a lot of difference.

The program demonstrate the life time of variables in the main module, the classes, and module code. By-The_Way I don't understand why you wants to terminate and reload a activity. Not bothered by your reasons, I choose a different approach. The difference between use and abuse is very thin. I'm going to use / abuse the lifetime of a class variable because we have full control over the lifetime of a class object. Combined with smart shifting of the values between the activities produces the following result.

The program use a global variables in a code module, load it in the main module during start-up where you can update it. I take a start text only for demonstration
purposes, be free to start with a empty text string or what ever you want.

When you start Activity1 it take this global value from the code module, then you can update the value during the life time of Activity1. Reload of Activity1 is simple re-initialize and update the input filed with the value of the global code module value.

From Activity1 you can start Activity2 which also take the global code module as the default text field input with each (re)load). In Activity2 you can also update the value of the global text string variable. Again the start value is the value of the global code module value.

I show you how simple you can make a fault. Because the main process is still in the background, the global code text value is not updated from Activity2. So if you close Activity2 and return to main process, the original value is still displayed on the screen. However if you start Activity1 from the main activity, then the updated text value from Activity2 is showed in Activity1 because there is now update of the text field in the resume sub defined in the main activity. Off course you can build an update mechanism when the global code module text string is changed.

In a have loaded Android environment less imported processes can be killed. As far as I correctly understand, the Starter process will not be killed. So if you put important variables there, they keep always alive.

Is there a way, to get a 'virgin' Activity2 each time I load it?
@Pflichtfeld I hope for you that how I putting away in the different modules and activities and the way in which they are used will give you a usable 'virgin' reload (sorry I'm just quoting šŸ¤­ )
 

Attachments

  • force sub-activity.zip
    14.6 KB · Views: 167
Upvote 0

Pflichtfeld

Active Member
Licensed User
@ MicroDrie: Thank you for your detailed help and for the little example. I knew, how this works, but your example shows it very clear.l
@all: It seems, that only now I understood, that Activity_Pause means exactly what it is called: Pause. Not unload.
Thank you all!
 
Upvote 0
Top