android finish() vs activity.finish() - android

I am working on some legacy code for an android app, and in some activities when they want to finish the activity they write
finish()
and in other places they write
activity.this.finish()
what is the difference?

actvity.this is transparent in an Activity class, because it references to itself, so you can call a class method both using and not using it as in Java

Related

Overriding back button on android when it does not extends activity

Hi currently I'm having an augmented reality application and I'm using the open source code of DroidAR.
My problem right now is that in the augmented reality screen the code extends Setup ( which in provided by DroidAR ) instead of extends Activity like what we normally do. Thus when I wanted to override the back button I could not use onBackPressed. Is there any other way that I could override it?
Not familiar with DroidAR, but according to the source code of Setup, it has a "target" Activity that you pass as a constructor param (and can retrieve by calling getActivity()).
I suppose you could just simply override onBackPressed() in the Activity you're passing to the constructor of Setup (and if you would like to call it explicitly, you could just invoke getActivity().onBackPressed()).

android Activities classes and objects

I am new to android development.
So I have a question about activities.
On Google developer, they say you have to make a class which is inheriting from the class Activity.
So far so good. Now I am thinking an Activity is a class, which leads to my major problem.
So when activity is a class how can it invoke all the live cycle methods.
In Java, I learnt there must be an object of a class to invoke methods and actions. So when activity is not an object but a class, how is this possible.
Kindest regards
Jan
When a user starts your app the android operating system will instantiate an object based on your class which must be a subclass of Activity.
The operating system will use that object and call the lifecycle hooks (e.g. onCreate, onResume, ...) which you implented in your activity subclass when one of the related lifecycle events happens.
Here it is clearly mentioned under CREATING AN ACTIVITY header, that system calls the callback methods, based on which event has occurred. Suppose user is interacting with your activity, then using the object created by the OS for that activity, all the callback methods will be executed. Please refer.

get onResume/onPause/onDestroy from non activity

There is a class in a library that is called from javascript (html5 app). It can't be an Activity extender.
The class has access to Activity and WebView objects.
Is it possible to get onResume, onPause and onDestroy activity events from this library class?
On API Level 14+, you can call registerActivityLifecycleCallbacks() on your Application instance to find out about all callbacks to all activities in your app.
Or, modify your Activity to call the class in the library on each of those callbacks.
Mouse right click Source --> Override/Implement Method you can find methods which you need it. But make sure add your library on your project

using onBackPressed in android

I am working on an android application project which has more than 8 activities just up to now. I need to learn how back button behave in android. Because I need to override it and do some actions when it pressed. When back button pressed android looks up the trace file and go to the activity which you came from to the current activity. However in my application you can go to an activity from several other activities. And I should know from where i came to this activity so that i can decide whether I should override onBackPressedmethod or not. But I don't want to do this with carrying some parameters with something like putExtra and startActivity. Is there a better way to handle this problem? Thanks
Sure override onbackpressed and use intent to traverse to what ever class you want to move.
You could try doing a BaseActivity and putting your custom logic there.
All of your 8 activities would extend that BaseActivity that has custom behavior there.
From that BaseActivity you can do whatever logic you want in onBackPressed based on a custom variable that you would send.
I wouldn't recommend overriding onBackPressed ever. Check the other lifecycle methods: onPause or onDestroy.
If you want to do something like a custom navigation you should check the ActionBar goUp instead.
You can find out which activity launch this activity by calling getCallingActivity ()

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