Check If An Child Activity Is Instantiated Or Not

This example explains how you can check if child activity is instantiated in your app or not.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it CheckInstalledApp.

2.) Create and write following into AndroidSlaveActivity.java:

 

package com.example.checkinstalledapp;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;

public class AndroidSlaveActivity extends Activity {

TextView textOut;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.slave);

textOut = (TextView)findViewById(R.id.output);

Toast.makeText(AndroidSlaveActivity.this,

"onCreate", Toast.LENGTH_LONG).show();

Bundle bundle = this.getIntent().getExtras();

if (bundle==null){

textOut.setText("Self start without bundle");

}else{

String textPassed = bundle.getString("key");

if(textPassed == null){

textOut.setText("No text passed");

}else{

textOut.setText(textPassed);

}

}

}
}

 

 

3.) Create and write following into slave.xml:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello World"

/>

<TextView

android:id="@+id/output"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

 

 

4.) Write following into main.xml:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello World"

/>

<EditText

android:id="@+id/input"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello World"

/>

<Button

android:id="@+id/startalave"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Start Slave App"

/>

</LinearLayout>

 

 

5.) Run for output.

Steps:

1.) Create a project named CheckInstalledApp and set the information as stated in the image.

Build Target: Android 4.4
Application Name: CheckInstalledApp
Package Name: com.example.CheckInstalledApp
Activity Name: CheckInstalledAppActivity

checkinstalledapp1

2.) Open CheckInstalledAppActivity.java file and write following code there:

 

package com.example.checkinstalledapp;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CheckInstalledAppActivity extends Activity {

final static String PACKAGE_NAME = "com.test.AndroidSlave";

final static String CLASS_NAME = "com.test.AndroidSlave.AndroidSlaveActivity";

EditText textIn;

Button btnStartSlave;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textIn = (EditText)findViewById(R.id.input);

btnStartSlave = (Button)findViewById(R.id.startalave);

btnStartSlave.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View arg0) {

String textToBePassed = textIn.getText().toString();

startSlave(textToBePassed);

}});

}

private void startSlave(String passingText){

Intent intent = new Intent();

intent.setClassName(PACKAGE_NAME, CLASS_NAME);

Bundle bundle = new Bundle();

bundle.putString("key", passingText);

intent.putExtras(bundle);

//check if the slave app installed

List<ResolveInfo> list = getPackageManager().queryIntentActivities(

intent,

PackageManager.MATCH_DEFAULT_ONLY);

if (list.size() >0 ){

//Yes, the Slave App installed

startActivity(intent);

}else{

//No!

Toast.makeText(CheckInstalledAppActivity.this,

"Slave App not yet installed!",

Toast.LENGTH_LONG).show();

}
}
}

 

 

3.) Compile and build the project.

Output

checkinstalledapp2

checkinstalledapp3