Writing an Android GUI using Python (7:Activity)

Activity of android application is main entity which likes windows on win32 platform. It has lifecycle, contains gui widgets, and manages them together. Activity can create child activities, with initial parameters, and gets results from them. Here gives an example to operate activities, which code

Introduction

Activity of android application is main entity which likes windows on win32 platform. It has lifecycle, contains gui widgets, and manages them together. Activity can create child activities, with initial parameters, and gets results from them. Here gives an example to operate activities, which code is written with python based on wrapandroid project. The purpose of this article is to tell programmer how to operate activities using python, rather than explain activity’s lifecycle and how to create child activities, which is talked in detail in android sdk documents.

Examples given in this article is simple. We create two activities, one is root, and another is child. The root activity contains an edit widget, which is used to get input from user, and a button widget. When users press button, child activity is created with input as parameter. Child activity displays the parameter in text widgets, shows an edit widget for user to input result for parent. At last, the text returned from child is shown in parent activity.

Root Activity

Layout XML file

We use xml file layout. The xml file contains text view, button and edit view, which is listed below.

Step 2: The step 2 is to obtain widgets defined in the layout file. And set onClick event listener of button. When the event is triggered, we build an intent and create child activity. The child activity is named ChildActivity. It must be declared in AndroidManifest.xml. Otherwise, the call will be failed. AndroidManifest.xml:

Code of child activity

The boot code of child activity is same as parent activity.

code.py

Step 1:

The first step of python code is to get service group object and service object created by java code, which is the same as parent activity. Python uses global namespace, which should be pay special attention. If we use same variable name as parent activity, it will replace the variable in parent, which may cause error.

First, we get start intent, which is set by parent. Then, obtains text view defined in the layout file, and show string of the intent in the text view. Gets button widget, set it’s onClick event listener. When the event is triggered, we build result intent for parent and call finish function to end the child activity.

It is hardly to tell where is the problem. Is there any error message shown in the logcat window?Further more, you can debug using source code of wrapandroid, using follow steps:1. remove wrapandroid.jar2. download source code from http://wrapandroid-for-multilanguage.googlecode.com/files/wrapandroid_src_and_examples_0_9_0.rar[^]3. add java files in "com\srplab\wrapandroid" into your project src directory.4. set break point in "public void onClick (View v)" function of file StarEventView_OnClickListener.java5. In python file, "code.py" and "childcode.py", make sure "setOnClickListener" function is called. myButton.onClick = myButton_onClick; myButton.setOnClickListener();

Hi li9705, the article indeed is good, what is missing to me is a link to, or a brief explanation of, the requirements for Python, used IDE, and any other info that may come with.I've been quite confused landing here from a search engine trying to understand what is your environment and attempting to run the code.