Detecting activity launching inside application - android

I'm trying to realize functionality for login launched activities names. I don't want to add checking in every activity. Short time ago i saw that it's possible to realize something like "activity lifecycle manager" inside application and catch callbacks like activityCreated(...) or activityDestroyed(...). But unfortunately i forgot the exact name of that method/interface(i don't even remember what was that :( ) and i didn't manage to find something similar here.
So, can somebody tell me how can i achieve this?

You can extend the Application class and implement the ActivityLifecycleCallbacks interface:
public class MyApplication extends Application implements ActivityLifecycleCallbacks {
// Various activity callbacks here
}
You can read about the callbacks here.
The above class will need to registered in the <application> tag of your manifest file under android:name:"" property as android:name:"MyApplication".

Related

What is an AbstractActivity in android?

What is an Abstract Activity in android? This question was asked at one of the interview. I tried searching about this at androidxref unfortunately not able to found.
Can any one help to answer this , Thanks!!
There is no such term in Android system called AbstractActivity.
Abstract activity is any activity which has been marked as abstract.
And like any other abstract java class, it cannot be instantiated and hence it will fail if passed as intent to startActivity() method. Also Android Studio will not allow you to declare such activity in the manifest file.
These abstract activities are mostly used by some android libraries to declare abstract methods and provides any method implementations useful for its task like login mechanism.
One of the advantage of this approach over an interface is that it can make use of activity callback methods.
As in comments the purpose of AbstractActivity is same as abstract class in java, which cannot be used directly(direct instance creation is not possible).
Using abstract activity you can define a group functionality for app activity screens.
For example,
LoginScreen: Abstract activity holding some functions defined
UserLoginScreen: specific ui/function for common user
AdminLoginScreen: specific ui/function for admin user

How to access a start of Application (not Activity)

I need to implement very specific code in the start of the application.
I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.
I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities.
This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.
I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.
Therefor, I need to access the start of the application, or that you have another solution for me.
Thank you !
The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
You need to extend application class.
public class AppApplication extends Application{
#Override
public void onCreate() {
super.onCreate();
//Do whatever you want
}
}
And this AppApplication class should be included in manifest file.
<application
android:allowBackup="true"
android:name=".AppApplication"
android:icon="#mipmap/ic_launcher"
I need to implement very specific code in the start of the application.
Every time when Android "gets a request" to start any of your app component (Activity, Service, BroadcastReceiver) and your app isn't running yet, it forks the app_process (a.k.a zygote), changes its name to your.package.name defined in AndroidManifest.xml, initializes an Application instance, calls its onCreate() method, then instantiates the component requested and calls its lifecycle methods (Activity's onCreate(), Service's onCreate() or BroadcastReceiver's onReceive()).
There can be only single instance of Application class which lives untill the app process dies. That said, any class instances you create within your extended Application class will also live until the app process is killed by the system.
Example: Understanding the Android Application Class

Extending the Application class in android twice

Is it okay or possible to extend the Application class in android twice and have two childs of it in an application? And also why do we need it & what's the purpose of extending it?
Is it okay or possible to extend the Application class in android twice and have two childs of it in an application?
You can only have one registered Application subclass in the android:name attribute of the <application> element. You are welcome to create as many subclasses of Application as you want, but only one will be used. However, you are welcome to have MyApp extend Application, have MyOtherApp extend MyApp, and register MyOtherApp in the manifest.
And also why do we need it
Few apps need it. Quoting the documentation, "There is normally no need to subclass Application".
what's the purpose of extending it?
If you have application logic that needs to be executed every time Android forks a process for you, Application is a common place to trigger that logic. For example, most crash logging libraries (e.g., ACRA) have you configure them in an Application subclass, so that they can handle crashes from everywhere else in your app, for every one of your processes.
You want something like this?
MyApp extends Application
MySecondApp extends MyApp
This is possible. There are several usecases for this. For example define an Application for your App but override it for a specific build type or product flavor (e.g. debug). Or a custom Application for your (Android) Tests:
MyApp extends Application
MyDebugApp extends MyApp
MyTestApp extends MyApp/MyDebugApp
Define each Application Class in the according AndroidManifest:
MyApp => main package
MyDebugApp => debug package
MyTestApp => androidTest package
Yes, it's possible. The reason extend classes is to reuse code written in the parent class and extend its functionality.

How to bypass activity declaration in AndroidManifest.xml?

I'm working on code that will be stuck inside other people's apps. I have two activities that both play off of a video view (with slightly different behavior). However, i'd definitely like to NOT rely on other people manually having to declare AndroidManifest.xml activities that i create. (some developers might just forget to declare it in their manifest)
So essentially, i'd like to be able to tell android to load an activity which is not declared in Manifest. How do i do this?
here are my thoughts:
1) I've tried subclassing a declared activity (declared activity referring to it being declared in AndroidManifest). however, calling this subclass throws a ActivityNotFoundException not surprisingly.
2) i COULD pass in a static view via a static method like:
public class Blah extends Activity {
private static VideoView badIdea;
public void setBadIdeaView(VideoView vv) { badIdea = vv; }
//... start it up as usual, but `badIdea` is now configured
}
but i really don't want to do this because a view holds on to a context, and i DO NOT want a static strong reference to an Activity context.
3) does anyone know how Android actually loads your activities? i'm guessing Android uses reflection to open an instance of the class.... but... i mean, why do activities have to be declared in a Manifest? is this for security purposes in order to prevent bad dynamic classloading? is this a possible solution?
thanks
How do i do this?
You don't. All activities must be declared in the manifest.
However, i'd definitely like to NOT rely on other people manually having to declare AndroidManifest.xml activities that i create.
As suggested in a comment, if your code is implemented as an Android library project, you can try relying upon manifest merging.
some developers might just forget to declare it in their manifest
So? They should catch it in testing. You can also add sanity-checking to your exposed API, where you see if the app has your activities registered, by means of PackageManager, so that way your code will "fail fast" if they did not follow your instructions.

Unable to instantiate a nested class ( extends broadcastReceiver ) when a sendBroadcast is made

I am developing an application in Android and have an issue where Android framework throws java.lang.InstantiationException upon trying to instantiate a nested class.
Here is the scenario.
Class A extending Activity with nested class B which extends BroadcastReceiver:
public class A extends Activity
{
public void onCreate(){
....};
//Class B
public class B extends BroadcastReceiver
{
public void onReceive(...)
}
}
Class declarations in manifest file :
....
Sorry for the bad indentation. Just couldn't find a way of properly indenting the code with so many tags.
Broadcast from a service :
Intent updateIntent = new Intent();
updateIntent.setAction("desired_action");
sendBroadcast(accountPropertyUpdateIntent);
With all the above given things, the code gets compiled, but when run on the device, after the broadcast is called, i get a InstantiationException saying cannot instantiate pacakage.A$B, and dalvik says no found.
Now this whole scenario works on Android 2.2, but somehow this fails on 2.1.
I don't know exactly what is happening. Am in need of help. Maybe something basic is missing by me.
Can anyone please help me? Thanks in advance.
Finally got a logical conclusion about the whole scenario myself. Was dumb enough not to read the <receiver> documentation on developer.android.net.
It clearly stated that there are two way for an application to get the broadcast.
There are two ways to make a broadcast receiver known to the system:
One is declare it in the manifest file with this element.
The other is to create the receiver dynamically in code and register it with the
Context.registerReceiver() method.
See the BroadcastReceiver class description for more on dynamically created receivers.
I am using both the methods.
My scenario should have failed on 2.2 as well, as those receivers were registered through manifest too and they should have been called automatically, but somehow it didn't ( This still remains a mystery ).
Removed all the receivers from manifest and just kept the dynamic registration of broadcast receivers and now the code works just like before only without the exceptions.
Thanks for all the help. :)
Nested class documentation says (last paragraph):
To instantiate an inner class, you must first instantiate the outer class.
Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
So Android can not instantiate inner class automatically. Just move class B to top-level (into separate file B.java).

Categories

Resources