Overriding back button on android when it does not extends activity - android

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()).

Related

android finish() vs activity.finish()

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

How is application working without onCreate method?

I've imported google drive quick start project and it's MainActivity contains these functions:
com.google.android.gms.drive.sample.quickstart.MainActivity.onActivityResult(int, int, Intent)
com.google.android.gms.drive.sample.quickstart.MainActivity.onConnected(Bundle)
com.google.android.gms.drive.sample.quickstart.MainActivity.onConnectionFailed(ConnectionResult)
com.google.android.gms.drive.sample.quickstart.MainActivity.onDisconnected()
com.google.android.gms.drive.sample.quickstart.MainActivity.onPause()
com.google.android.gms.drive.sample.quickstart.MainActivity.onResume()
How does it work without onCreate method?
First of all I think you mean Activity onCreate method, not Application onCreate as you mentioned in the title. It is not the same. com.google.android.gms.drive.sample.quickstart.MainActivity implements android Activity therefore it contains default implementation of onCreate().

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 ()

Extending Android Classes In Eclipse

From what I can tell, Eclipse only supports making "skeleton-code" for very few class extensions in Android (such as Activity). If I want to extend TextView, Fragment, etc. I have to start completely from scratch and provide my own skeleton code.
Is that true, or am I missing something simple in Eclipse that creates skeleton code for various class extensions?
If by skeleton code you mean overriding the methods in the super class, then try the following:
In the package explorer, right click on the class, go to the Source tab and select Override/Implement Methods.... This will give a list of the methods you can override and implement.
Alternatively, go to a new line in the class editor outside of an existing method and press control + space to bring up a list of methods you can override. This is faster when overriding only one or two methods.
There is no difference between Activity or TextView when creating a new class from the Eclipse wizard.
By default all methods that need to be implemented will be auto-generated (constructors, abstract methods, interfaces' methods)
Say you class extends TextView.
Eclipse will ask you to override certain required methods in your class.
If you want to override some additional methods.
Right Click on the class name TextView. Goto Source and select Override/Implement methods. Then select the methods you want to override in your own class.

Android: Using methods from an Activity in a Widget. Extend Activity and AppWidgetProvider?

I'm working on an Android app which has an activity and a widget. This is currently implemented via two classes in two .java files - one for the activity (extending Activity), one for the widget (extending AppWidgetProvider). Nothing out of the ordinary here as far as I'm aware...
However, the widget class code could be a lot simpler if it was to make use of functions and asynctasks defined in the activity class. Duplicating these functions seems like bad design, so I'm wondering how I can structure the app to make them usable?
Can I extend both Activity and AppWidgetProvider somehow? Can I import one in the other?
Thanks!
either make the funcs static, or make a 3rd class to hold these funcs
Move the functions down into a service. Create a Service and you can use context.startService(Intent) from you WigetProvider or from the activity to access the functions.

Categories

Resources