How to launch a certain activity of an Android app - android

I would like to launch, from my app, two specific activities A_Activity and B_Activity from apps Aapp and Bapp
I inserted two buttons and in the two OnClickListener I wrote
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("com.Acompany.Aapp.A_Activity");
ctx.startActivity(intent);
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("com.Bcompany.Bapp.B_Activity");
ctx.startActivity(intent);
Moreover I added to AndroidManifest.xml the following lines
<activity
android:name="com.Acompany.Aapp.A_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Acompany.Aapp.A_Activity" />
</intent-filter>
</activity>
<activity
android:name="com.Bcompany.Bapp.B_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Bcompany.Bapp.B_Activity" />
</intent-filter>
</activity>
But my app crashes and in the logcat I read "No Activity found to handle Intent"
Where is my mistake?
EDIT: More precisely the two activities are not in my own app

More precisely the two activities are not in my own app
You should investigate target app's manifest file first, to check if these activities are available to others by being exported or offering publicly accessible intent filters. It looks you may simply be not allowed to do what you attempt to.

Try like this. For your home activity(first launch activity) do like this in your manifest file.xml
<activity
android:name="com.Acompany.LaunchHomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.Acompany.LaunchHomeActivity" />
</intent-filter>
</activity>
<activity android:name="com.Acompany.Aapp.A_Activity">
</activity>
<activity android:name="com.Bcompany.Bapp.B_Activity">
</activity>
don't include <intent-filter> to all activities

Intent intent = new Intent(myFirstClass.this, MySecondClassA.class);
startActivity(intent);
Class B
Intent intent = new Intent(myFirstClass.this, MySecondClassB.class);
startActivity(intent);

Related

Reload Just the Main Activity not the Launcher Activity

I'm using this code to refresh MainActivity.java when a Refresh button is pressed.
Intent intent = getIntent();
finish();
startActivity(intent);
MainActivity.java has the category default and there is another Launcher activity. So whenever I press the Refresh button the Launcher Activity also starts again. I only need to start the MainActivity class. Below is the manifest
<application
android:allowBackup="true"
android:icon="#drawable/play_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name="com.theanilpaudel.joshilo.FirstScreen"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theanilpaudel.joshilo.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What you are looking for in this case is an explicit Intent that launches MainActivity.
You can do this like so:
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
This will ensure that the Intent only launches MainActivity.
However, I highly recommend re-evaluating why you need to completely restart the Activity to refresh it. There are much cheaper (faster and less resource intensive) methods of updating data displayed in an Activity.
I guess I know the culprit. Try using the following code once.
Intent intent = getIntent();
startActivity(intent);
finish();
As far as I know startActivity() uses context to run. In case if you call finish() before startActivity() that may lead to destroy context of MainActivity and will get the application's context i.e. fires the activity with behavior LAUNCHER or MAIN.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Let me know if it helps or you still have difficulty reloading activity.
Cheers

android how to start the main activity when I am on another activity

I am on signin activity and if the user click a button I want to start the main activity.
this is the mainfest for the main activity welcome
<activity
android:name=".Welcome"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I tried like this
Intent welcomeIntent =new Intent("android.intent.action.MAIN");
startActivity(welcomeIntent);
but i got a screen saying complete acting using with many choese , what is the solution pelase?
Change your intent to directly launch your class
Intent welcomeIntent = new Intent(context, Welcome.class);
startActivity(welcomeIntent);
If you're in an activity replace context with this

How to launch app at the same time on Android

I have two apps, A and B.
I want to call B by A.
My code in A is as below:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.example.bapp","com.example.bapp.BActivity"));
intent.putExtra ("test2abc", "abctest2");
startActivity(intent);
And B's intent filter in manifest as below:
<intent-filter>
<action android:name="ACTION_BACKCALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
But it will close B before launch B while B had opened.
I find below code to open *.txt file. This will open two txt reader app at the same time.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);
startActivity(inte
nt);
How can I arrive that?
hey i found this code snippet Launch an application from another application on Android which will work for you
set the address of the application which you want to launch
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
you might want to try android:launchMode="singleInstance" and
android:finishOnTaskLaunch="true" while defining launcher activity.
<activity
android:name="com.example.test.sampleMediaPlayer"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:finishOnTaskLaunch="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For Activity A.

call another application activity from different application activity?

Intent intent = new Intent();
intent.setClassName("another_app_package_name","another_app_package_name.class_name_in_that_package");
startActivity(intent);
getting ActivityNotFoundException ?
How to solve this problem? This problem posted earlier as well but no solution.I manifest file of application whose activity to be called i have used intent filter as well.
Activity to be called by some other application's activity
<activity
android:name="com.example.custompermission.PrivActivity">
android:permission="abc.permission.STARTMYACTIVITY">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Check this
Intent intent = new Intent();
intent.setClassName(another_app_package_name.this,another_app_package_name.class_name_in_that_package.class);
startActivity(intent);
and in manifest.xml add this
<activity android:name="another_app_package_name.class_name_in_that__package" android:configChanges="orientation|keyboardHidden"></activity>
In manifest file you do like this:
<activity android:name="another_app_package_name.class_name_in_that__package"></activity>
you should add the activity in manifest file, which you are tried to called from first activity.
you need to add both the activities in manifest file:
for ex:
Intent intent = new Intent (HomePage.this, Second.class);
startActivity(intent);
in manifest: <application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".HomePage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name =".Second"></activity>
</application>
Once, clean the project and run.

ActivityNotFoundException?

I am getting an ActivityNotFoundException in the following code:
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
I've had this issue too, as perfectly concisely described by jpahn.
the period at the front did not give any help to me.
even with exactly this (a copy of the original question including edits), I would still get ActivityNotFoundException.
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
This was resolved, after much trial-and-error, by simply adding this to the intent-filter in the manifest:
<category android:name="android.intent.category.DEFAULT" />
So the final manifest file contained:
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I got this error after moving an activity class from one package to another.
Clean build solved it (Project -> Clean).
Be sure to declare your activity in the manifest.xml within the aplication:
<application>
<activity android:name=".YourNewActivity"/>
</application>
To start the new Activity:
Intent intent = new Intent(main.this, YourNewActivity.class);
startActivity(intent);
Where main stands for the current activity,
Add a . (dot) before your activity name in Android Manifest. So it should be android:name=".WordsToSpeakMainActivity"
I have some addition to the #Tom Pace answer. The answer is completely right, but to make it more clear:
ActivityNotFoundException occurs because of absence of
<category android:name="android.intent.category.DEFAULT" />
Because when Android OS see this in the manifest file, understands that this activity can receive intent.
The point ActivityNotFoundException thrown is that, when activity(intent-creator-activity) tries to create intent for other activity(intent-receiver-activity), Android OS sees there is intent for receiver activity but receiver activity does not receive anyone. Then Android OS returns null or empty intent to intent-creator-activity. And startActivity throws that exception.
I have found a code from android developers to avoid this exception:
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
Android Developers: Intent Filters
To be safe you can also call your new activity like this:
Intent intent = new Intent();
intent.setClass(this, THECLASSNAME);
startActivity(intent); //
However, you must add the activity to the androidmanifest - and write a . in front of it, e.g.
<activity android:name=".YOURACTIVITYNAME"></activity>
There two types of intents in android framework,
1-Implicit intents that you are using,
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
just add one line in intent filter
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
2- Explicit Intents
Intent i=new Intent(CurrentActivity.this,WhereWeWantToGoActivity.class);
startActivity(i);
To launch an activity by a string definition, use:
Intent intent = new Intent();
intent.setComponent(
new ComponentName("com.app", "com.app.activity.TheActivity"));
startActivity(intent);
At the very top of your AndroidManifest.xml, you'll see the package attribute
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.example"
and then, in the activity tag, you'll see the name attribute:
<activity
android:name=".Something"
Make sure that the package name and activity name, when joined together, make the full package specification of your Activity i.e.
com.android.example + .Something = com.android.example.Something
Otherwise, you'll get a ActivityNotFoundException.
I found a solution to this problem... I´m using 2 modules in a android studio project, the thing here is that I needed to add the activity to the main manifest file
<activity android:name="com.HeadApp.ARTry.UnityPlayerActivity"
android:clearTaskOnLaunch="false" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
/>
I had that in the unity activity manifest, I just copied the activity and paste it in the main manifest and that was it, hope it helps, eve been struggling a lot with this for the past 3 weeks

Categories

Resources