I'm new to Android app dev, and I'm trying to use a library (LocaleHelper) that says to extend my App class:
class App : LocaleAwareApp() {
}
And to extend my base activity class:
open class BaseActivity : LocaleAwareCompatActivity() {
}
but I don't have either of these in my Android Kotlin project. All I see for source code generated is the MainActivity class.
I'm using Android Studio 3.4.2 with min sdk set to 22. Is there a different way to integrate this library?
You have to create a new App class like this:
class App : LocaleAwareApp() {
}
and in AndroidManifest add this line inside the application tag:
android:name=".App"
After that you create a BaseActivityClass:
open class BaseActivity : LocaleAwareCompatActivity() {
}
and your MainActivity and every other activity you create should inherit from this BaseActivity like this:
class MainActivity: BaseActivity() {
}
For more customization you should look into https://github.com/zeugma-solutions/locale-helper-android
App (I assume you mean Application) and Activity classes are part of the std android library, so you don't have to "have them". Extending them means you have to write your own implementation with "extends Activity" and "extends Application" in class prototype.
more info here: https://medium.com/#balakrishnanpt/android-application-class-a8a1d64c82d1
hope that helps
Assuming that you mean this library, LocaleAwareApplication is a class in the library. You would create a subclass of it in your project:
class MyAwesomeApplication : LocaleAwareApplication() {
}
Then, in your manifest, in the <application> tag, add an android:name attribute that points to your subclass:
<application android:name=".MyAwesomeApplication"
// other attributes go here
>
I do not know why the README has the wrong class name (LocaleAwareApp instead of LocaleAwareApplication).
Related
This question already has an answer here:
Extending Android Application class
(1 answer)
Closed 1 year ago.
As Android documentation said:
All apps that use Hilt must contain an Application class that is annotated with #HiltAndroidApp
When I create an Android project from template (Tabs template of Android Studio) it seems not to provide any Application class but class MainActivity : AppCompatActivity() instead. So how can I use Hilt in this project?
I have tried this:
#HiltAndroidApp
class MainActivity : AppCompatActivity() {
...
}
But it didn't work :<
Your MainActivity is not the Application class.
For activities use the #AndroidEntryPoint. See more on https://dagger.dev/hilt/android-entry-point
The annotation #HiltAndroidApp is for the Application class. See more on https://dagger.dev/hilt/application
Android application using Xamarin but I think this applies to non-Xamarin. I read this article while doing research.
In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.
In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.
Previously, I did not need to extend the application class so I just had my manifest handle it like this article says. However, I recently had a need come up to extend the application class so I can use the OnCreate event for the application.
I added a new class which extends Application and it looks like this:
namespace MyApp.Droid
{
public class MyApp : Application
{
public MyApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
:base(handle, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
//Do OnCreate things here.
}
}
}
It seems just adding this class is not complete. First question, do I also have to modify my manifest now? Second question, the class can have attributes for theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?
Here is the manifest:
<application android:allowBackup="true" android:icon="#drawable/mainapplogo" android:label="#string/app_name" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="#style/AppTheme">
Thank you!
First question, do I also have to modify my manifest now?
In this question if you mean just because you have added the new application class is there a need to make any change in the existing manifest then NO there is no such requirement.
Second question, the class can have attributes for the theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?
The Android Manifest's Themes and everything else will stay there, The Android Application class and AndroidManifest.xml work independently and hence adding an extended application class does not require any change in your Manifest file or vice versa.
Good luck,
Feel free to revert in case of queries
UPDATE
See to it that your application class has the [Application] attribute on top of the class name
#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MyApp: Application
{
I am having an activity that extends appcompatactivity I want to use the methods of activity like getwindow(), setRequestedOrientation() and finish() etc. but not able to do so... It says cannot resolve method. so, is there any way cast AppCompatActivity to Activity.
I am using Android Studio 3.0 Canary 4
For a minimum API level of 15, you'd want to use AppCompatActivity. So for example, your MainActivity would look like this:
public class MainActivity extends AppCompatActivity {
....
....
}
To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:
compile 'com.android.support:appcompat-v7:22:2.0'
You can use this AppCompat as your main Activity, which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).
The BigNerdRanch book is a good resource, but yeah, it's outdated. Read it for general information on how Android works, but don't expect the specific classes they use to be up to date.
You can cast AppCompatActivity to Activity:
((AppCompatActivity) getActivity()).someAction...
I am having two classes in my android project so, should I mention two activity tags in my app manifest file or will it work without including the activity tag.
Although, my app is working fine but I think this creates error - Unfortunately, your app has stopped working.
it depends Which kind of classes you have.. if your class is extending Activityor ActionBarActivity or AppCompatActivity or the class wich extends any of these, than you must define it in manifests.xml file. otherwise if your class extends nothing or just extends view, then no need to define it in manifests.xml file.
Yes,every Activity you used must be declared in manifests.xml,or when jump the activity,app will crash.
I am using
Android global variable
example.
How do I add another "application" tag in AndroidManifest.xml ?
According to the documentation, only one application tag can be inserted in the manifest.
Quote from doc:
"Only the manifest and application elements are required, they each must be present and can occur only once."
If you're following the example at the given link, just add the android:name and android:label XML element to your current application tag and it'll work just fine.
Example of my application tag in an application I'm developing at the moment:
<application
android:name=".Globals"
android:debuggable="true"
android:icon="#drawable/icon"
android:label="#string/app_name" >
Globals is my application class.
I know this question was asked a long time ago but using Android Studio 3.0 and higher, I came across the same problem and did not find anything online that helped.
After a lot of research and my own testing, I found out how to successfully implement two or more classes that both extend android.app.Application into the Manifest's <Application/>.
First Class File:
public abstract class MyClass1 extends Application {
// ...
}
Second Class File:
public class MyClass2 extends MyClass1 {
// ...
}
Manifest File:
<application
android:name="com.mydomain.myapp.MyClass2">
<!-- Rest of code here... -->
</application>
how to handle multiple application classes in android
When a library is already extending the "Applicaton" class just do the following
class lib1 extend Application {
...
}
class lib2 extend lib1{
...
}
in the manifest -> <application android:name = "package.lib2".....