Tuesday, January 22, 2008

Did you ever fall in loop when traversing through activities, like one activity launching another and the other launching the previous one, then back pressing returns to that activity. This can be prevented very easily, when you think that the second activity is no more required, you just can call finish() function of that activity. This will solve some problems, though this looping happens only to bad programmers like me :D. Here is an interesting thing, when a subactivity finishes it's job it can send data back to the main activity who started it, for this the main activity has to override a function like this

@Override
protected void onActivityResult(int requestCode, int resultCode,
String data, Bundle extras) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data, extras);
if(requestCode == SOME_INTEGER_WHICH_WAS_USED_DURING_CREATION_OF_ACTIVITY_AS_INTEGER_PARAM+OF_INTENT){
String text = "";

// This is a standard resultCode that is sent back if the
// activity doesn't supply an explicit result. It will also
// be returned if the activity failed to launch.
if (resultCode == RESULT_CANCELED) {
text ="Canceled";

// Our protocol with the sending activity is that it will send
// text in 'data' as its result.
} else {
if (data != null) {
text = data;
}
}
}
}


So when finish() function is called from subactivity, this function is called in mainActivity. This code is executed during returning from subactivity class.


setResult(RESULT_OK, "SOMETEXT");
finish();

Most of the codes are copied from APIDemos.

Launching an activity is not all. After launching another subactivity/activity you might want to send some data/parameter to that activity. This is done in a new way known as Bundle. To launch an activity we create an intent. Suppose our intent name is "intent". We can put data with the intent like this

intent.putExtra("keyName", "somevalue");

We can add multiple entries here. This is a key,value pair. So to receive this data from the receiving activity we have to write this code

Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}


This is how we can send data to another activity, here simple string is sent, complex structures like array can be also sent through bundle. Didn't use those as i am a bad bad programmmer and very bad at reading documentations ;).

Sunday, January 6, 2008

First create a class extending some activity(Activity/ListActivity/MapActivity etc).
You have to put this activity reference in AndroidManifest xml file like this


< activity class=".nameOfTheClass" / >


so that android can find this activity.

To lauch an activity from an activity, here is a simple code.

Intent i = new Intent(this, newActivity.class);
startSubActivity(i, ACTIVITY_CREATE);

Here Activity_create is a simple integer declared.

I don't know the details so i am not going into those, i understand nothing, i can do copy paste well :D. And google is there ;).

Saturday, January 5, 2008

It's so much easy to show map in Android, just 4 lines of code. In eclipse, create an Android project, in the projects main class extend it by MapActivity. Then in main layout file (main.xml) add the following lines


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />


The java file will look something like it

public class MapViewer extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
}


Fix imports by pressing Ctrl+Shift+O
Run the project, u will see the map, no key event is attached so use your mouse to
move or zoomin/out.

Here is the source code

Friday, January 4, 2008

Happy New Year


New year, new journey, new company , new jobs , new works , new learning , new tensions and new challenges. Everything is new but we can't forget the good old days !!!

Happy New Year , let the game begin.

Recently i was in a nasty problem. Whenever i started my emulator it was showing some error message and said that it could not start this/that. And whenever i wanted to access contacts it was showing exceptions. So i started to do what i do, that is googling.

After some googling i decided to remove all the applications installed in my emulator. And that was very easy too. After erasing it all worked well.

Now how to erase those apps,
Go to the home directory of your android sdk through command prompt. Then go to tools. Then in command window type "emulator -wipe-data" and press enter. This will remove all the datas/apps installed in emulator. It solved my exception problems.

How to erase single application from command line:

First,make sure the emulator is running.Then follow below steps:
1. go to tools directory in command
2. adb shell
3. cd /data/app
4. ls (It will display all the .apk installed in your emulator)
5. rm ***.apk(which you want to remove)
6. exit


Check this link

Here is a good article on how to launch an activity during startup of the emulator

launch on startup

You can test the startup by going to the home_directory_of_android_sdk/tools/ then run the emulator.exe, you will see your startup application running after loading the emulator.