Thursday, October 13, 2016

When you should call an Activity

There are several places where you need to call an activity:

1. When you touch your app icon in home menu screen you need to call an activity to show the first page of your app ( for example facebook app shows login page). Because this app icon resides in home menu screen(not inside your application) you need to ask a question who is responsible to call this activity or who call this activity that makes it appears?. The answer is the Manifest.xml file handle this. look at the example of manifest.xml code below :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
In the code above notice the line <category android:name="android.intent.category.LAUNCHER" />. This is the line that makes MainActivity is launched first rather than other activities when you click the app icon in home menu screen. It means that MainActivity is launched first not because this manifest contains only one activity but because this LAUNCHER line. You can add more activity in this manifest by putting more tags <activity></activity>. But you should remember that these more activities except the LAUNCHER will never be called (shown to users) unless you call it explicitly in java code, I will give you the example in point 2 below.

 2. You need to call an activity if you want to go from one activity (one page) to another activity (another page). for example in facebook app in login page after you click login button the app will go to home page. At this case you need to call home activity explicitly after the login button click event. this code below show the example:

loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
            }
        });
Now pay attention to these two lines : Intent intent = new Intent(MainActivity.this, HomeActivity.class); startActivity(intent); This is how you call an activity in java code. intent object specify where or what activity we are right now (in this case MainActivity) and what activity to show next (in this case HomeActivity). the method startActivity(intent); do the actual work to start activity.

Besides these two places you can start activity, there are also several places where you need to start activity for example when you click something in notification drawer,  the technique to start activity uses IntentFilter. I will explain it in later articles.


Thank you for visiting our website. Just comment below if you have any question to ask.

No comments:

Post a Comment