emi
emi

Reputation: 2950

Button to open another app not working

I'm learning Android. I created 2 app (A and B). App A works ok. Now I created app B with just 1 activity and a button to open app A. But the button in my app B doesn't work on clicking it. This is what I did so far.

All the following files are for app B:

MainActivity.java:

package com.example.appopner;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Button;




public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    Button appOpener = (Button) findViewById(R.id.button1);
    appOpener.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent i = new Intent(Intent.ACTION_MAIN);
            PackageManager firstApp = getPackageManager();
            i = firstApp.getLaunchIntentForPackage("com.assignment.projecttomiko");
        }
    });
}

activity.main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.appopner.MainActivity"
    tools:ignore="MergeRootFrame" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="67dp"
            android:text="Open First App"
            android:onClick="appOpener" />

    </RelativeLayout>

</FrameLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appopner"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

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

</manifest>

I will appreciate any help :)

Upvotes: 0

Views: 70

Answers (1)

MJ93
MJ93

Reputation: 5296

Below

i = firstApp.getLaunchIntentForPackage("com.assignment.projecttomiko");

You are missing a single line:

startActivity(i);

Upvotes: 3

Related Questions