Android Question Activity.Finish issue

joneden

Active Member
Licensed User
Longtime User
Hi All,

After calling Activity.Finish I return to my dashboard activity but then opening the activity again has the FirstTime flag in Activity_Create set to false. So it seems that the activity wasn't destroyed in the Activity.Finish call.

Should I do something different that Activity.Finish or is there a way to start the activity and force the FirstTime to be true? I've been using StartActivity to get it running...

Thanks for any pointers

Regards

Jon
 

Cableguy

Expert
Licensed User
Longtime User
Activity.finish does not destroy the activity, it only tells the OS that it can reuse the allocated resources when it needs to.
When an activity goes into background, by pressing the home button for example, the OS still keeps most of the resources for that activity. Eventually the activity will be killed and disposed of if it stays long enough in the background. The only difference between this and the finish method is timing. With finish the kill will eventually come sooner that if in background.
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Thanks both.

So after that first time it is never true for the full life cycle of the app? I'd thought that it is true when loading the activity from a non existent (or destroyed) state.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks both.

So after that first time it is never true for the full life cycle of the app? I'd thought that it is true when loading the activity from a non existent (or destroyed) state.
It is True when the OS allocates memory to your app and loads its code. So it is false when the app was already in memory when you launched it. If the OS kills it for the reasons explained by Cableguy, then FirstTime will be True again.
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Ah that sounds quite a lot like I expected then. It's an app with a lot of activities and when one finishes I want it to be properly destroyed which from the sounds of it WOULD then allow the FirstTime flag to set back to true.

So if that is right the question is how do you force Android to destroy an Activity within the running app?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Ah that sounds quite a lot like I expected then. It's an app with a lot of activities and when one finishes I want it to be properly destroyed which from the sounds of it WOULD then allow the FirstTime flag to set back to true.

So if that is right the question is how do you force Android to destroy an Activity within the running app?
ExitApplication should destroy the application.
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Don't worry about it. If the Activity is no more needed the system the system will destroy it.
But "when" belongs to Android, if you run in low memory it will be sooner.
Normally you need Activity.Finish only for special conditions.

Example 1:
You have three Activities in your app.
Your app start with Activity 1.
Then calling StartActivity(Activity2).
Activity 1 goes in the background and Activity 2 is showing.
In Activity 2 you call StartActivity(Activity3).
Activity 2 goes in the background and Activity 3 is showing.
If you now press the Back button Activity 3 is closed and you go back to Activity 2.
Activity 3 is now no more used and Android may destroy it (sooner or later).

Example2
Same three Activities.
Your app start with Activity 1.
Then calling StartActivity(Activity2).
Activity 1 goes in the background and Activity 2 is showing.
In Activity 2 you call now
Activity.Finish
StartActivity(Activity3).
Activity 2 is closed and Activity 3 is showing.
Activity 2 is now no more used and Android may destroy it (sooner or later).
If you now press the Back button Activity 3 is closed and you go back to Activity 1 (because Activity 2 don't exist anymore for the app, destroyed or not).
Activity 3 is now no more used and Android may destroy it (sooner or later).
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Thanks All,

Cant use ExitApplication as I want to kill the Activity not the app.

The timing of the auto destroy is the issue - my user may end up viewing one activity, goes to another and then back in fast succession before Android even considers killing the first one. As such I was looking at an explicit way to force the pace.

Thanks for the diagram, it's a reaffirmation that my mental image is right.

It's looking like I will need to have some form of a work around for this as relying on Android to destroy the activity isn't a good idea. A rare occasion where having a lower powered device would probably help things :)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You can use the closed event to redirect explicitly wich activity to show.

This was you can have your user going from activity 1-> 2 -> 3 -> 1
Without having to kill your activity. Set a prevActivity to know from where the activity was called, and the choose what to do accordingly
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks All,

Cant use ExitApplication as I want to kill the Activity not the app.

The timing of the auto destroy is the issue - my user may end up viewing one activity, goes to another and then back in fast succession before Android even considers killing the first one. As such I was looking at an explicit way to force the pace.

Thanks for the diagram, it's a reaffirmation that my mental image is right.

It's looking like I will need to have some form of a work around for this as relying on Android to destroy the activity isn't a good idea. A rare occasion where having a lower powered device would probably help things :)
I don't understand why you want to kill the activity. I understood that you want FirstTime set to true but you did not explain why this is needed in your app. To speak frankly, I never used FirstTime in my own apps. I prefer to test IsInitialized of an object before initializing it, or test if the content of this object is null.
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
I've done a custom version of the custom list view and it has sortable columns, a menu based filter and a pager. Once I added it to a couple of activities I then looked at all of the code that was being duplicated on each page, the filter being a prime example. I moved all those variables out to a library so that I didn't have to muck around with them on the activity so much.

So now I have the activities, a class and a variable lib.

Because each activity stores the default sort column name in the variable lib I need to reset it each time the activity changes.. I think I'll just make it so that if the user exits the activity the variable gets cleared. Then on start if it is blank the activity knows to do the reset that it would have done if FirstTime was true...
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Thanks Erel, that starter service looks interesting although this is more an issue of in app activities causing me issues than external access.

Thanks everyone for the pointers. I think I'll do the work around of resetting the filter on activity exit.
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Cant use ExitApplication as I want to kill the Activity not the app.

The timing of the auto destroy is the issue - my user may end up viewing one activity, goes to another and then back in fast succession before Android even considers killing the first one. As such I was looking at an explicit way to force the pace.
Look at my example in post 8. It doesn't matter if the Activity is destroyed or not if you call Activity.Finish before StartActivity().
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Thats odd then as I am intercepting the back button and calling activity.finish there. Then when starting it it is startactivity.

I think that the key is the "sooner or later" bit - I need it "now". :)
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
If you press the back button the current Activity will be finished, no need to catch the button and call Activity.Finish there.
If you don't want to go back to the last Activity then call Activity.Finish before StartActivity().
Did you read post 8?
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
With my new 'fudge' I do the reset in the back so still need it although good to know that the activity.finish isn't needed.

Yeah but may have missed the point initially. You're using the activity.finish and immediate startactivity to force the killing off of activity2 right?
 
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Yeah but may have missed the point initially. You're using the activity.finish and immediate startactivity to force the killing off of activity2 right?
Yes.
I have this behaviour often in my apps: from Activity 1 to Activity 2 to Activity 3 then back to 1 ignoring 2.
 
Upvote 0
Top