Android equivalent of: void main() / Sub Main? - android

I am trying to make my Android application run some code when run from the launcher, BEFORE launching into an activity. That is to say I want my app to start with a Sub Main as opposed to going into an Activity first.
Essentially, in pseudo, I want to do something like this:
void main() {
doSomeInitializationStuff();
startActivity(myFirstActivity);
}
According to this question, it looks like Android does not have this concept literally. So I was looking at creating an invisible Activity as my entry point, but cannot figure out how to make an activity invisible. I've tried these two methods, which seem to be the only ones coming up in my searches, but they don't seem to actually do anything...
this.setVisible(false);
this.setTheme(android.R.style.Theme_Translucent_NoTitleBar);

Instead of creating an invisible activity you can create a splash activity and start all your initializations there .
I have not tried this but you can extend the application class and use onCreate in your application class to initialize what you need.
Here is the JavaDoc for onCreate of the application class
/**
* Called when the application is starting, before any other application
* objects have been created. Implementations should be as quick as
* possible (for example using lazy initialization of state) since the time
* spent in this function directly impacts the performance of starting the
* first activity, service, or receiver in a process.
* If you override this method, be sure to call super.onCreate().
*/
public void onCreate() {
}
You will need to let the app know that you are using a custom application class by using the the android:name parameter in the tag of the Android manifest file.

In most of the Android application there is concept of SplashScreen one can use that screen to accomplish such behaviour and the real motif is of this SplashScreen is to proccess such tasks in background while advertising for the app itself and various things related to that

One option would be to not have an invisible Activity, but a SplashScreen. This has the advantage that the user already sees something happening when the app starts up, so that he does not get the impression it is no working. For an example see e.g. this class; you would put the doSomeInitStuff() at around line 54
Otherwise I think, you can just not load a layout in onCreate() of the first activity and then forward from there.

Related

Activity starts with onResume but next onPause

I have a strange behavior of Activity in complicated legacy code.
In LoginActivity I want to start some PromotionActivity but just after onResume it goes to onPause.
As this app has some ActivityUtils in one of its numerous libraries - I used another approach:
I called one default PublicActivity and passed a fragment there.
It looks like
Map<String, String> params = new HashMap<>();
params.put("fragmentName", PromotionFragment.class.getName());
ActivityUtils.showActivity(activity, PublicActivity.class, params);
And all is shown without problems.
To understand why this works, I copied PublicActivity from a library to the main folder as a PublicActivity2 and tried to launch it but failed again. I did different changes, even extended PublicActivity2 from PublicActivity
public class PublicActivity2 extends PublicActivity {}
ActivityUtils.showActivity(activity, PublicActivity2.class, params);
but again failed.
How is it possible that PublicActivity is shown without problems but PublicActivity2 which is extended from PublicActivity and used in the same showActivity method - only goes to onResume and immediately goes to onPause?
I found the issue. As it's complicated legacy code with numerous libraries I didn't notice that there is some Application.ActivityLifecycleCallbacks in one of them which listen lifecycle of activities and compare every launched activity with a list of permitted ones. So, I found that list, added my activity and all works fine.
I didn't find it quickly because it was passed from a library to main app as another interface which extends needed one, luckily searching through the app with a word Lifecycle I found getApplication().registerActivityLifecycleCallbacks(new Analytics().getActivityLifecycleCallbacks())
So, for this weird situation when your activity immediately finishes after launching without any visible reason - my advice is to look for this ActivityLifecycleCallbacks

Android InputManagerCompat.InputDeviceListener custom class not receiving callback on any method

I have an Android application that uses a class extending the following type:
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class GamePadController extends View implements InputManagerCompat.InputDeviceListener {
// The current device that is controlling the ship
private InputDevice mInputDevice;
private int mDPadState;
...
When I run the apk in a mobile sometimes the Gamepad is captured in android and my custom methods onGenericMotionEvent and onKeyUp are called.
It is a random behaviour, sometimes my methods are called and my behaviour is executed but sometimes only the default behaviour happens (like if my classes were not registered).
Things I observed:
In any case when I run the app it takes some time for my methods to
be called, like if there was some lazy loading that I should force
sooner (maybe).
The code my changes is based on did work, but I introduced
other things that don't look related at all (other classes, a thread)
No exceptions or errors in the LogCat, already made sure of no empty catches and things alike
Do you have any clue or advice on this behaviour?
Thanks in advance
So it seems like I was putting the callback methods in a specific View within the Activity (which had other elements like textviews), meaning that only when that specific element was focused the input was captured in the callback... that is why the documentation tells you to put the callbacks in the Activity or the View... probably it made more sense to put this behaviour in the activity so it was captured no matter what was in focus.

I'm a noob and I'm used to writing reusable code with methods. How do I write the same kind of code with Activities?

I'm writing my first android app, and it's going very well so far, but my code is getting obtuse and I'd like to reorganize it in a way that allows me to reuse portions, and add things more easily.
Based on my previous experience writing simple command line programs that call methods, this is how I THINK I should organize my code:
(some code in MainActivity)
Call a void method of the object DoStuff:
Launch Activity1 and write some values to SharedPreferences file, THEN
Launch Activity2 and write some values to SharedPreferences file, THEN
continue running code from MainActivity
Right now Activity1 and Activity2 both launch at the same time. Is there a different way I should be writing/organizing my code? I guess I'm trying to do thing with Activities that I'm used to doing with methods. But I'm aware that my thinking might be wrong on this. I hope this makes sense.
Thank you for your help!
Your understanding of Activities is wrong. Activities do have methods that you can very well use.
An Activity is basically one screen that you see in your app. It can be started, stopped, resumed, etc. You can have different screens shown in one Activity (e. g. with Fragments).
If for example you have a list of notes in one Activity, you could have the detail of one note shown in another DetailActivity.
Only in rare cases, for example if you want to check on startup what Activity to show you could have another activity that does not have a layout but only does some checks and launches another one.
In each of your activities you can have methods to execute what you want on user interaction. Of course this can also go into other classes.
I would recommend you to start with a basic Android tutorial to gain a better understanding of the concepts.

How to make a core of activity on android?

I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.

Android launcher

I need to start android application not with Activity^ but with some controller class that will start some activity
Is it possible?
I'm not sure if I understand your question correctly, but an Android application is built up by four "components" as mentioned in the "Android Application Fundamentals", http://developer.android.com/guide/topics/fundamentals.html (no, you don't need all four of them make your application work).
The most common way of starting an application (and actually the only one I've been in touch with) is to define an Activity in your applications AndroidManifest.xml file as described on the link above. NOTE! that an Activity doesn't have to define a UI; you are not obligated to call the "setContentView()" function. Hence your "controller class" can extend Activity and be the very Activity you define as the start-up Activity in your manifest xml. You can then call "startActivity()" function with parameters to start any other Activity, whenever you see fit, from your controller class (this is also described in the link above).
Hope this helps.
Either create a GUI-less activity without calling setContentView() or use a BroadcastReceiver that accepts launcher intents (action=MAIN, cateogry=LAUNCHER). In Activity.onCreate or receivers callback method you can place logic which will invoke the actual activity of choice.

Categories

Resources