Binding a whole native Activity to Xamarin.Android - android

So, I have a certain activity coded in pure Android that I want to use in my Xamarin.Android project. I compiled the activity into a ".jar" file, created a binding project, referenced it and so far everything was working. But when I try to create an intent to navigate to that activity, it returns an error:
System.TypeLoadException
Could not resolve type with token 01000013 (from typeref, class/assembly BR.Com.MyProject.Project.UI.SignUpPageActivity, MyBindingProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
Anyone have any clue on how to fix it? The binding is working fine, because along the activity I bound a few other classes, and they are all working, the only exception is when I call the "typeof()" method.
I've searched the whole documentation and found nothing about.
My Metadata.xml:
<attr path="/api/package[#name='br.com.myproject.project.ui']/class[#name='SignUpPageActivity']" name="visibility">public</attr>
Code calling the activity:
public void NavigateToSignup()
{
Context context = Application.Context;
Intent intent = new Intent(context, typeof(SignUpPageActivity));
StartActivity(intent);
}

Related

Atooma Intent based trigger

I am trying out the Atooma SDK. They provide an abstract class IntentBasedTrigger for creating triggers that fires when the intent fires. The class has an abstract method
public String getIntentFilter() throws RemoteException
In the docs (http://www.atooma.com/developers#start/trigger) They use this with a standard Android intent. However, I want to use it with a custom Intent, so I made up a string and returned it from getIntentFilter. However, this does not work (I can't be more specific at the moment) which leads me to believe that defining intents is a bit more complicated?
IntentBasedTriggers works with custom Intent as well.
I am part of the Atooma team, unfortunally we don't have a plugin sample with IntentBasedTrigger, but you can see the SnapBack plugin using it
Here is the source code
https://github.com/SnapbackLabs/AtoomaPlugins/blob/master/AtoomaBlowDetectionPlugin/src/com/atooma/plugin/blow/TR_BlowDetectionSensorBased.java#L92

What is the difference betweeen Intent(getApplicationContext(), class) and Intent(class)

I'm working on a code as a reference and encountered this Intent call: Intent(getApplicationContext(), secondActivity.class)
I got baffled because in my projects I only use: Intent(".mYSecondActivity")
I tried removing getApplicationContext() and changed it to Intent(".secondActivity")' in which the name is registered in the manifest. I run the application and Forced Closed. What is the significance of getApplicationContext()?
secondActivity.class is coded to retrieve data from a PHP MYSQL database.
"This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you."
source: http://developer.android.com/reference/android/content/Intent.html#Intent%28android.content.Context,%20java.lang.Class%3C?%3E%29

calling another project class from another android?

I want to call a class of another project. I added it in the build path and also declared that class in the manifest file but when I call it gives me a no class found error. I am calling it from intent.
Intent intent = new Intent(getApplicationContext(), org.coolreader.CoolReader.class);
intent.putExtra("path", adapter.getItem(position).getPath());
startActivity(intent);
If you want to launch CoolReader from your program than look at this answer:
Intent coolReaderIntent = getPackageManager().getLaunchIntentForPackage("org.coolreader.CoolReader");
coolReaderIntent.putExtra("path", adapter.getItem(position).getPath());
startActivity(coolReaderIntent);
Of course, CoolReader (program) should be installed on the device.

Calling an Application Activity from a Library Project in Android

Ok,
So I'm making a library project of UI elements. The Library has some activities which are based of ActionBarSherlock which is a backwards compatibility library for the action bar in android. In these activities I would like to have a button in the action bar which will take the user home regardless of which activity they are using in the Library project.
Some terminology. The 'library' refers to the Android UI library project I'm working on. The 'Application' refers to whatever customer a developer might be using with the Library included.
Usually, when you make an activity and you want to call another, you would do something like this.
intent = new Intent(this, WhateverMyActivityName.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Simple enough. But here's the tricky bit. Android Libraries have little to no knowledge of what application is using them. So 'WhateverMyActivityName.class' is useless as there is no way to predict what the developers application will call their activities.
I need to replace
intent = new Intent(this, WhateverMyActivityName.class);
with something like this
intent = new Intent(this, getApplication().MainActivity().getClass());
or possibly use some sort of intent action which will call the main Activity in the application (Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER)
So in short: How do I get an applications main activity from a library project?
We can use reflection to get class object.
Class.forName("com.mypackage.myMainActivity")
Add this code in Library project to call,
try {
Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
startActivity(myIntent );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.
The application calls some method in your library providing the Intent to be invoked, or providing the Class of the activity to be invoked. Your library stores that someplace and uses it.
Your assumption that the right answer is "Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER" may be inaccurate. For example, some apps have that be a splash screen activity (which is an issue in its own right, but that's beside the point), and that would not be where a home affordance within the app should go.
You can get the list of activities using the code mentioned in the this post. After that loop through the resolveinfo and check the intenet filter to find the activity with your desired intent action.

Using an Android library project Activity within another project

I have an Android library project that I would like to use from within another Android project.
The library has a Activity declared in its AndroidManifest. When I try the following within the second project:
Intent intent = new Intent(this, ReaderActivity.class);
startActivity(intent);
I get the following exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.digitalpages.reader.demo/br.com.digitalpages.reader.demo.ReaderDemoActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
...
How can I open the Activity from the another project?
EDIT:
By users answers I added the following line into my second project
<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
But it's still doesn't works
I believe you must include the <activity> in your own AndroidManifest.xml -- I don't think it gets picked up from a library. I don't have my reference for that handy.
Update: It's official solution. From the doc:
Declaring library components in the manifest file
In the manifest file of the application project, you must add
declarations of all components that the application will use that are
imported from a library project. For example, you must declare any
<activity>, <service>, <receiver>, <provider>, and so on, as well as
<permission>, <uses-library>, and similar elements.
Declarations should reference the library components by their
fully-qualified package names, where appropriate.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello",
"classname//ex-com.hello.ExampleActivity"));
startActivity(intent);
And make sure in library you have declared the activities. You don't need to declare the library activities in your current project's manifest.
did you add to the manifest?
http://developer.android.com/guide/topics/manifest/uses-library-element.html
This works:
In your library, put your custom Activity:
public class MyLibraryActivity extends ListActivity { ... }
No need to put it into a manifest.
In your calling Android project, create an empty (dummy) wrapper:
public class MyActivity extends MyLibraryActivity { }
and add reference to this class to your manifest:
<activity android:name="my_package.MyActivity" ... />
I am aware that the question is quite old but I think this might help people like me that came up with the same problem.
Using Eclipse, the best way to share code and activities among libraries is probably the one that can be found in the android documentation here:
Managing Projects from Eclipse with ADT
When using an activity inside the library, the activity should be declared only inside the manifest of library.
The activity can be started from the main app like this:
Intent intent = new Intent();
intent.setClassName(this, "com.duna.remotelocklibrary.activities.MainRemoteActivity");
startActivity(intent);
I tried to start the library's activity like the following code, but i warn you: it doesn't work
Intent intent = new Intent();
intent.setClassName("com.duna.remotelocklibrary", "com.duna.remotelocklibrary.activities.MainRemoteActivity");
startActivity(intent);
you should import just the code of the activity ( not the manifest too ) , and then , declare your Activity ( of the library ) , in the manifest of your second project
You don't need to explicitly add activity in your main project's manifest if you have already added the activity in your library's manifest by using the following code when starting library's activity.
For Kotlin
val myIntent = Intent(activityContext, ActivityToLaunch::class.java)
// Needed to set component to remove explicit activity entry in application's manifest
myIntent.component = ComponentName(activityContext, PickerActivity::class.java)
activityContext.startActivity(myIntent, PICKER_REQUEST_CODE)
For Java
Intent myIntent = new Intent(activityContext, PickerActivity.class);
// Needed to set component to remove explicit activity entry in application's manifest
final ComponentName component = new ComponentName(activityContext, PickerActivity.class);
myIntent.setComponent(component);
activityContext.startActivity(myIntent, REQUEST_CODE);

Categories

Resources