Android Tutorial Problems With App Closing On Back Button Press? (Tutorial)

Ok so this was one of the first issues I ran into when first starting in B4A. I want to write this tutorial to help people out that might have the same issues as I did. So lets start.

Whenever you create an Android Project, you should already know to create a layout, if not this can be done by going to designer and then making a layout and saving it. So ill call this layout examlayout. Now you need to load this layout in order for it to work properly. Here is what the code looks like.

B4X:
Activity.LoadLayout("examlayout")

This code should be placed under your activity create. Now is where I made my mistake. I would go into the designer and keep making layouts and then load a layout, for me it was when a listview item was clicked. That is where I made my mistake you need to define your layout on a new activity. So for this example I will just be using a simple button.

Go to your designer and add a button to your examlayout. Once you have added a button save your layout but do not close it out. Go to tools and click generate members and then click on the click event for the button and hit ok. You now have a button with a click event and we are going to have it so when you click on that button you can go to the nextevent and then when you press the back button it will return you to your examlayout.

Now go to project>Add New Module>Activity Module and then I want you to name it test. This will create a whole new activity for you. Now we have to tell the button how to get to that activity. So under your button click add this code.

B4X:
StartActivity(test)

Now your back button should work just fine. Dont forget that when you want to make a layout for the test activity you use the same loadlayout code. Well I hope this helps.
 
Top