Skip to content

Android Intents

Intent

Intent is data structure that specifies the description of an action to be performed. It is a message to request an action from another app component. Android uses the information contained in Intent object to specify which component to launch. Intent objects provide information about an action to be taken and the data necessary for launching that action.

Different Forms of Intent

1. Explicit Intents

Explicit intents are used where you know exactly the class name of activity or service to start.

Example:

If you want to launch the activity represented by the class say OurActiviy.class, you can launch this activity by using the following statement:
startActivity(new Intent(this,OurActiviy.class));  //read more about startActivity()

The above statement is valid only when the activity you want to launch is within the same android project as your current activity in which you are using the above statement.

2. Implicit Intents

If you want a action to be performed by the component from another app, you have to use implicit intent.

Advertisements

Android wrap_content and fill_parent

The fill_parent and wrap_content are opposite of each other.

1. wrap_content is used for those components that just need to occupy enough space to display their content. If android:layout_width and android:layout_height for the given component are set to wrap_content its widths and height both do not take extra space more than required by their content.

Example:

<Button
android:id=”@+id/myButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”My Content”/>

In the example above the button will have enough width and height to hold the content “My Content”.

2. fill_parent is used where the component is needed to be displayed to as big as its parent. The component may fill its parent length wise, width wise or both as required. In the example below the button will be wide enough as its parent component, and its height will be long enough just to hold its contents, “My Content”.

Example:
<Button
android:id=”@+id/myButton”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”My Content”/>

Further reading:

http://www.mkyong.com/android/android-wrap_content-and-fill_parent-example/

Redirect to Mobile Version of Website

If you have created a mobile version of your existing website. And now you want to redirect users with mobile devices to that version of website. Below is the javascript code to change the location to the page specifically created for mobile users.

1. <script type="text/javascript">

2. <!-- 

3. if (screen.width <= 699) {

4. document.location = "mobile_site.html"; }

5. //-->

6.</script>

In the line number 3 of above code

(screen.width <= 699)

screen.width represents total width of the user screen, in pixels.

In line number 4,

document.location = "mobile_site.html";

"mobile_site.html"

is the location of your mobile version of the website.

Happy mobile programming……

Android NDK

What is Android NDK?

Android NDK is a set of tools that allows Android developers to implement parts of their apps in native-code languages like C/C++.

Android NDK can be useful for some apps, where developer wants to reuse existing code written in languages such as C and C++. Another advantage will be the increased performance in some cases.

Benefits:

1. Helpful to use in tasks that are self contained, and CPU intensive
that don’t allocate much memory, such as signal processing, physics simulation etc.
2. If used in appropriate situations, it results in increased performance.
3. You can reuse code written in C/C++ resulting in less app development time.
4. Java Native Interface (JNI) can also be used to access native code, but it is hard process. In that case, you have to compile everything on your development machine for target architecture.

Disadvantages:

1. It increases app complexity.
2. It is really difficult to write proper and effective NDK code unless you are very strong at C/C++

So it is suggested to use Android NDK only in situations where Android Framework APIs have limitations.

Thanks

Welcome To saiful103a's World

In my previous tutorial I have talked a little bit about intent. So we have our intention to do something either explicitly or implicitly now it’s time to start those intent. And we do this via startActivity() or startActivityForResult() but why do we have two kind of method for this.

As name suggest startAcitvity will only take you there where you wanna go without worrying about getting any result from child activity to parent activity.
It’s real classic when you have a sequence activity to go through, like filling some information in every activity and that’s it.
What if you want an activity that uses information from other activity or may be other application. In that case you need more than just startActivity(). And that’s when comes startAcitvityForResult().
So it start another activity from your activity and it expect to get some data from child activity and return that to…

View original post 508 more words

Android: startActivity and startActivityForResult

Here are some differences between startActivity and startActivityForResult

 

1. startActvity()

startActivity() will start the activity you want to start without worrying about getting any result from new child activity started by startActivity to parent activity.

For use of startActivity() with intents read Android Intents

2. startAcitvityForResult()

startAcitvityForResult() starts another activity from your activity and it expect to get some data from newly started child activity  by startAcitvityForResult()  and return that to parent activity.

Example:

To start an activity ( for example, MySecondActivity ) using startAcitvityForResult() following statements can be used

 

private static final int SECOND_ACTIVITY=0;
Intent myIntent = new Intent(this, MySecondActivity.class);
startActivityForResult(myIntent, SECOND_ACTIVITY);

The statements above creates an intent that is used to initiate the activity MySecondActivity.

startActivityForResult ( ) has two parameters, first is

Intent myIntent, which is used to start MySecondActivity. And second parameter is
request code SECOND_ACTIVITY which is returned to our calling activity through a call to
onActivityResult(int requestCode, int resultCode, Intent intent)

onActivityResult ( ) has three parameters passed to it, first is requestCode which was provided through startActivityForResult ( ). This request code is used to recognize the activity in case there are more than one child activites started through startActivityForResult ( ) method.
Second parameter to onActivityResult ( ) is resultCode which is used to determine the result of activity as completed or cancelled etc.
And the third parameter to onActivityResult ( ) is intent through which different type of data can be attached by the child activity.

I will provide detailed examples in my next upcoming posts.