I want to test if the Activity by name "test" exists or not in the appliaction. May be using Manifest file before calling startActivity or in its catch block for the following:
Starting an Activity with Intent and SetClassName
So say:
if(test.exists){
Activity is registered and start it
}
else{
create Activity and add it in manifset programmatically, as this would be a dynamic Activity
}
We cant add activities in manifeast file programmatically even if you get it from JSON response.
And if you are launching the native Activity we usually dont add them at all to manifeast file but for certain things we need to add permission to use them in our app.
use forName instead..
something like this..
try {
Class<?> act = Class.forName("TestActivity");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
// do what you wish if not exist
}
Manifest and so do other xmls present in apk cannot be modified.So yoou may have to re-use your components and you you can use fragments for that(if i understand your requirements correctly). or simply reuse a activity,
Checking the presence of Activity can be done easily .
Related
The Android#startActivity(Intent) is specified to throw android.content.ActivityNotFoundException if there was no Activity found to run the given Intent.
I therefore have code like
try {
// Try to start activity provided by external app:
startActivity(intent);
} catch (ActivityNotFoundException e) {
// [...] Inform user about external app needed
// for this functionality to work.
}
which catches ActivityNotFoundException to handle the case where the external app is not installed.
However, from the Crashes & ANRs section of the Google Play Developer Console I'm starting to get crashes as IllegalArgumentException: Unknown component com.example.package/com.example.package.Activity. Should code invoking startActivity() be ready to handle that exception as well? Is this a framework bug (or documentation error)?
This exception generally arises when you haven't declared it in your manifest file. So, try doing it
Somewhere inside your application tag do this
<activity
android:name="Your_Activity_Name"
android:theme="Whatever your theme is"
</activity>
If its any other issue please let me know.
You can use this code to check it. This is the better approach then handling exception
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0)
{
// start your activity
}
I've added a reference to a new module and try to open an Activity from it.
It throws an Exception that says:
android.content.ActivityNotFoundException: Unable to find explicit
activity class{
com.giljulio.imagepicker.ui/com.giljulio.imagepicker.ui.ImagePickerActivity
};
have you declared this activity in your AndroidManifest.xml?
Do I need to add anything else beside reference the new module?
You have to define in gradle dependencies(in module where you want to call another module activity):
dependencies{
...
compile project(':yourModuleName')
...
}
After adding this sync the gradle and now can you use the activity in the module.
User like this. This will help you
Intent intent = null;
try {
intent = new Intent(this,
Class.forName("ir.test.testlibary1.HelloWorldActivity"));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Ok, so I am a few years late.
The issue is not that you don't have the gradle dependency as #arpit suggested, it seems you report a runtime exception. Also, what #Aman suggested will help with the handling the exception, it will just not help you start the activity.
If understood correctly, you have a multi-module app (let's say A-app module and B-lib module) and need to call another lib-module(C) from one B.
If that is the case, you need to declare that library's activity(C) in your module's(B) Manifest.xml, inside the tag.
If it has not already been setup, you also need to enable manifest merger.
How can I make a call without/with user interaction.
I found the intent Intent.ACTION_CALL on the internet, but I want to write program like the one which initiates a call in response of this intent, is this possible? If this is possible, then how can I do this?
I do not want to initiate a dialog to dial for a call, I want to internally make a call in my application.
Is there any java library, which I can use for this purpose?
Use Intent. That's how it is designed to work in Android:
try {
startActivity(new Intent(Intent.ACTION_CALL,
Uri.parse("tel:PHONE_NUMBER_HERE" )));
} catch (Exception e) {
e.printStackTrace();
}
This will start dialing.
That is not possible from an SDK application.You have to use Intent.ACTION_CALL.
I am developing an app were i need to use some sort of method to handel two different intents from a click of a Button. The first is only sometimes able to start without crashing the app. Therfore i need to start another intent were the app normally would have crashed.
Better explained do I need some sort of method that launches another intent, if the 1. intent can not start a new activity, then start the 2 intent.
I really appreciate some sort of formula, instead of a link or reference.
You are looking for a try catch block.
try
{
startActivity(intent);
}
catch(Exception e)
{
startActivity(another_intent);
}
Try/Catch in your code. You can then launch another intent if the first one fails.
If your application is crashing, you should sort this out. You wont be able to start another intent if the application crashes though, because the Applications Process has been killed by the OS. You will need to detect if you can launch the activity, otherwise launch another.
Fix the crash, then implement some switching logic based on what used to be causing a crash.
When I am going from one intent to another intent, I am getting this warning :
"Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy"
and the view stays on the old intent.
the code is :
btnCatalog.setOnClickListener(new OnClickListener() {
private ProgressDialog myProgressDialog;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myProgressDialog = ProgressDialog.show(introPage.this,
"Please wait...", "Doing Extreme Calculations...", true);
new Thread() {
public void run() {
try{
fetchAndStoreData();
Intent toMainPage = new Intent(introPage.this, mainPage.class);
startActivity(toMainPage);
sleep(5000);
} catch (Exception e) { }
myProgressDialog.dismiss();
}
}.start();
}
});
but when I comment the fetchandStoreData() method, then the intent moves to another intent.
the fetchAndStoreData() method is reading XML reading data from files and also saving data into SQLLite database.
so far I am having no idea why this warning is occurring.
need urgent help
Thanks
I had this error when "mainPage" (started intent) was not declared on Manifest file,
try to add :
<activity android:name=".mainPage" />
Another solution: check the onCreate, onResume, etc. methods of your Activity that is being opened. In my case, I had changed some code so that my onCreate method would call this.finish(); before the end of the method. I'm guessing some sort of race condition occurs when you do that and every time I opened my Activity I would get the same thing in logcat:
W/InputManagerService( 104): Window already focused,
ignoring focus gain of:
com.android.internal.view.IInputMethodClient$Stub$Proxy#46399630
Fix the activity that's being opened! If you need to close the activity immediately after opening, find some other way to do it; maybe via the runOnUiThread method, I don't know.
I had a different problem. I was able to change activities once, but not more than that. I think it was a combination of using Intent.FLAG_ACTIVITY_NO_HISTORY when navigating to the second Activity, and using Activity.startActivity to return to the first application. When I switched to using Activity.finish() to leave the second Activity and return to the first, everything started working better.
Got the same problem yesterday, due to a typo in the activity declaration on androidManifest.xml,but even after fixed the error persisted.
After restarting the Eclipse and the emulator I got this message in the console:once the main activity was launched:
"emulator: emulator window was out of view and was recentred"
and the secondary activity launched w/o problems.
Some special hidden char or somesuch in the mainifest.xml. Pasted from another manifest.xml:
android:screenOrientation="landscape"
After the problem, deleted and typed in manually. Then clean, build. And works. Fun.