Overview
I hope you got a chance to read some useful articles to start programming in Android application. Given below are some of the links:
- Android Programming - Day One
- Android Programming - Day Two
- How To Display A Dialog Windows In Android
- How To Display A Progress Dialog Window In Android
- Intent In Android
- Return Data Using Intent Object In Android Applications
- Passing Data Using An Intent Object In Android Applications
- Fragments In Android Applications
- Adding Fragments Dynamically In Android Application
- Fragment Life Cycle In Android Application
- Interaction Between Two Fragments
- Calling In-Built Functions in Android Application
So far, I have explained here about the use of an intent object to call other activities. Let's just recap and gain some more detailed understanding on how the Intent object performs its magic.
Introduction
In the article Intent in Android, we learned that we can call another activity bypassing its action to the constructor of an Intent object
- startActivity(new Intent(“com.example.administrator.Fragment2Activity”));
This action is known as component and it is used to identify the target activity/application that you want to invoke. It can be rewritten by specifying the class name of the activity if it resides in the project, like
- Intent intent=new Intent(this, MySecondActivity.class);
- startActivity(intent);
You can also create an Intent object by passing in an action constant and data, as follows
- Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));
- startActivity(intent);
The action portion defines what you want to do, while the data portion contains the data for the target activity to act upon. It can further extend and pass the data to the Intent object using the setData method.
- Intent intent=new Intent(Intent.ACTION_VIEW”); intent.setData(Uri.parse("http://www.google.co.in"));
In the above example, it is indicated that you want to view a web page with the specified URL. The Android OS will look for all activities that are able to satisfy your request. This process is known as intent resolution.
Coding
So, we have already seen how an activity can invoke another activity using the Intent object. Here, I will discuss the Intent filters. Here, I added some code in the existing app which I have covered in the previous article. Added a blank activity called BrowserActivity and code in AndroidManifest.xml file
- <activity
- android:name=".BrowserActivity"
- android:label="@string/title_activity_browser" >
- <intent-filter>
- <action android:name="android.intent.action.VIEW"/>
- <action android:name="com.example.administrator.Browser" />
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
The button is added to the activity_main.xml file.
- <Button
- android:id="@+id/buttonlaunchbrowser"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Launch C# Corner"
- android:onClick="onClickLaunchBrowser"
- />
onClick() method is defined in the MainActivity.java file.
- public void onClickLaunchBrowser(View view){
- Intent intent = new Intent("com.example.administrator.Browser");
- intent.setData(Uri.parse("http://www.c-sharpcorner.com"));
- startActivity(intent);
- }
Add a WebView control in the activity_browser.xml file.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- tools:context="com.example.administrator.myfragmentapp.BrowserActivity">
- <WebView
- android:id="@+id/webview1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
Add code for WebView control in the BrowserActivity.java class.
- package com.example.administrator.myfragmentapp;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class BrowserActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_browser);
- Uri uri=getIntent().getData();
- WebView webview=(WebView)findViewById(R.id.webview1);
- webview.setWebViewClient(new Callback());
- webview.loadUrl(uri.toString());
- }
- private class Callback extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading
- (WebView webview, String url){
- return(false);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_browser, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }



Kapi ShivharePosted Sep 28, 2016, 12:51 PM
hiio
Vignesh ManiPosted Aug 17, 2016, 7:59 AM
Nice
Praveen KumarPosted Aug 16, 2016, 11:20 AM
nice article
Purushottam RathorePosted Aug 16, 2016, 11:13 AM
Nice article. Thanks for sharing.
Ramesh PalaniappanPosted Aug 16, 2016, 8:31 AM
Nice content
Rajeev RanjanPosted Aug 16, 2016, 7:42 AM
Nice article sir... You have great collection of android apps development. Such a great and informative article series.
kalu singh raoPosted Aug 15, 2016, 2:18 PM
Nice