Android Question 1) How to prevent 'multiple instances' of a b4A application from being executed ? 2) How to remove all instances of a b4A app. from phone memory ?

beelze69

Active Member
Licensed User
Longtime User
Hi,

Sorry if I sound too basic.

1) How to disable 'multiple instances' of a b4A application from being executed ?

I mean the b4A application when executed again must say 'an instance of the application <Application Title> is already running.. this instance will exit now '... and exit ..

(something akin to App.PrevInstance functionality in vb6)...

2) In a b4A application, even after the end-user executes the 'Activity.Finish' and if we press the 'square symbol' button of the smartphone mobile (a RedMi 7 in this case) , the previous application screen is still shown and the application can always be invoked again.....

Now.. Is it possible to eliminate it completely so that once the Activity.Finish is executed, no 'instance of the b4A application' resides in the memory ...

Thanks...
 
Last edited:

Albert Kallal

Active Member
Licensed User
We are dealing with two questions. And they are quite much separate ones.

Lets address the first issue/question. There is no need to check or to have code prevent the user from launching the application more then one time. As far as I can tell, you can't do this. If the application is not loaded, then the OS loads the application. if the application was shoved aside, put to sleep not really running by the OS? Once again, flapping though the list of applications that are "sort of loaded", then that application comes back to life, and the "resume" life cycle triggers and you back in business.

And even if the application was/is sleeping? Clicking on the application icon will use + load that existing copy. So as far as I can tell, there is no issue, no worry, or no need to check or prevent a user from running two copies of the application. It simply does not occur, and no such code is required for this task in ANY way I can see,, or tell right now. In fact really big whopper of a question is how would one run two copies of a application if you wanted to! That's a challenge, and not really all that easy (or even possible) in most cases.

Ok, now we have the next issue/problem. That is of program exit and termination.

On iOS, you can't terminate the application. The user has to close it.

And Android OS also quite much controls this. It again is much the same.

First to answer this 2nd question? (how to exit)?

You have to try/test/show me how the other 100 applicaitons on your phone behaves.

ONCE you do the above, then you can now start to address this issue.

You find that if you use Activity.Finish, the results you get are the SAME as every other app you used, and have on your phone.

So, since the other 100 apps work this way - then yours will too!

Even if you use the ejection seat option (ExitApplicaiton), then the whole process is NOW in the hands of the OS and again beyond your control. You in fact not find any difference between ExitApplicaton and Activity.Finish (assuming we on the main form/view).

In this context (no real difference between ExitApplicaton and Activity.Finish. But you don't want to use ExitApplication in near all cases, since you need/want that activty to close down and return to the previous one (if you have one!!!).

However, ExitApplication used on main activity, or Actitivy.Finish have much the same effect at this point in time. So, again, just use Activity.Finish, and you are done, and that's all you can do, and in fact all you need to do.

So then?

If the user wants to flip though applications they been using (with the task switcher) and slide them off the screen, hit the "X" to dump and get rid of the app? They can. But YOU can't make that choice.

Again the OS controls this, and not you!

And in terms of activity.Finish? Well, ok, but better have some context here as to what that means, and when.

Since as a normal design, when I launch a new layout (a view, or I guess what desktop land people would call a form), then in a LOT of cases, that new view/form will have its own corresponding activity. That makes sense - it very much like a vb form, and its corresponding code module. The parallels between say a vb form + code behind, and that of a view and its corresponding activity is very simular.

There are some cases when I will load a layout - and stay in the same activity - but not all that often. The instant that new form/view has more then about 2-3 buttons, and code? Then you want to separate out that view/form from the existing activity and thus continue that concpdt of a form/view with its own code moule.

And this also works well since then Activify.Finish is your close form. And you don't even have to write ANY code for this to work (the user can tap the hard back button).

This means that more often then not for me?

The back button, or a button on the form to close the form? I only need the Actifity.Finish for my buttons on the form, but both that button or the hard back button will both do the same thing (a actitify.Finish occures).

And this means that my database save code runs in the Activity.Pause event. (and database load of controls from database occurs in the activity.Resume event. This is criticial for many reasons. One reason is if the user taps say the power button. Now the app sleeps. But on resume event, then how can I now re-fill the controls they were just editing? Well, since I use Pause to save, and Resume to load, then the user can:

Tap power button - data is saved.
hit back button to exit and return to previous form - again data will be saved.
Hit my back button or save button on the form - it also does a Activity.Finish (I don't put nor have the save code behind that button).
hit the hard menu button to go launch another application.
hit the hard menu button to show/display all running apps.

In all of the above cases? I don't have to write extra code SINCE I followed the correct event model that is built into the OS.

And once again, we let the OS do all the work for us. But this idea ONLY works if we follow the built in correct event model for the given activity. This is not really much different then learning over time say the event model for a vb form. (load event, close/un-load event etc.).

I point this out, since VERY often a Activity.Finish is not to close the application, or stop it, but simply close the form I am looking at and return to the previous form (and activity).

So, if they are on main form? tap the hard back button. You are done - that's it.

So, at that point, the OS takes over.

This problem, this issue is OUT OF YOUR hands and OUT of your control. It simply the way it works. You can no more change this then yelling at rain drops, and expecting those rain drops to change direction or stop follow. It just "is".

Try this with some existing applications, and see what occurs. What occurs with all apps is the above - it just how the phone works.

As a result? You really don't' need to do anything more then that Activity.Finish, and that occurs if they hit the hard back button and you did not even write one line of code here. So the hard back button on main, or a button on the form with Activity.Finish? They both do the same thing and the built in back button does this without you having written one line of code.

If you seeing one TINY bit different behvour with your applcations as opposed to the 100+ others you have installed? Well,, then perhaps you have a question. But then again, test your app in release mode as opposed to debug mode. Install it, close the app,, close the bridge. Heck, re-boot your phone.

Now, launch your app (without debug, then it don't need the bridge). Play with it, hit back button on main page. Now do this with any other application you have on your phone - you not see anything different - you simply will not.


Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0
Top