Android - Parent activity should determine what child activity had called it - android

I have 2 activities in my project, lets say Activity A, and Activity B. Both A and B extend the same superclass: BaseActivity.
We know that in this case, lets say if the Activity A is opened, then the superclass method onCreate() is called, and then the Activity A's onCreate() follows.
Being in the onCreate() method of the parent BaseActivity class, how can I determine which child activity has been started?

I would suggest a different approach. Basically don't do inheritance. Use composition instead. Remember most operations that you think you need to extend for really just need a reference to the context. Activities extend from context. So really you can provide most base functionality in any class that has reference to an active context. No need to do inheritance at all.
So if you want to share some functionality between ActivityA and ActivityB just put it in HelperC
HelperC.someOperation(Context c, otherParams)
HelperC can do anything that some base activity could do. Ultimately the base activity will never exist any way. It will always be an instantiated version A or B

I don't think you should do it this way.
As far as I remember the idea of extending, superclass method should contain only universal code. Puttin the differenting code in child classes would be much easier to do and as I believe more proper.
You can initilize some values in parent onCreate(), and then re-set it in childs'.

Related

Managing references to Singleton Classes in Android

I have a class in my app that extends the Application class. I have declared/named it in the Manifest.xml file. It basically initializes some stuff but more importantly gives me access to a serial port on the machine I'm programming on. Here is my question. I have an activity where I subclass this Application class. The Activity, lets call it Activity A initializes a variable that is declared in it's superclass (the one that subclasses Activity).
When I create an intent in Activity A meant to send the user to a different Activity, after calling startActivity(intent) I call finish(). Will that completely erase Activity A from memory, or, because in Activity A I reference a variable declared in the Application class, will this Activity A remain in memory
Nope, actually when we use superclass properties in sub class it won't be more in memory. So when ever we call for finish() for activity. All declared variables and all references related to that Activity will be destroyed (removed from memory).

Click Events inside fragment

I have a dashboard that is a fragment. Everytime I click a button, the dashboard is replaced by another fragment.
The click listener is implemented inside the dashboard fragment class. But I read somewhere that the better way to do it is to make the listeners inside the activity. Is it true? Why?
If yes, I can change it, i only have to copy the method in dashboard fragment to the activity, and make use of XML onClick feature.
I honestly can't think of a reason for declaring an onClick listener for a fragment in the activity.
First, fragments are suppose to be modular. Maybe you use it with this activity or that one. Putting the onClicks in the activity hardcodes a relationship between the two. Your activity is searching for the fragment, which isn't always there, and your fragment can't work except in that activity.
Second, where you declare your on click determines where it's implicit reference will be to. If you declare it in the activity, it can call activity functions, but It has no idea which fragment it came from. How does it reference fragment functions / data? Sure there's elaborate workarounds but why?
On the other hand, if you put it in the fragment, it can call the fragment functions. and it has the same life-cycle as the fragment (being attached to a fragment view), so the implicit reference isn't going to create a memory leak (by itself anyways). And if you want to call the activity, just use getActivity and cast it to your interface or subclass.

Android UI design pattern - Options Menu

I want to use my options menu on every activity that I have on my project.
So I've created an OptionsMenuActivity which inherits from Activity.
Each activity I've created inherit from it.
The problem is when creating MyPreferenceActivity which inherits from PreferenceActivity, I cannot use it.
What is the best way doing it ?
If all you are looking to do is find a place to put the onCreateOptionsMenu() and onOptionsItemSelected() methods you can create a separate class with those two methods, make an instance of that class a member of all your activities, and make these two methods 'pass through' methods in your activities, deferring to the member object that now handles the requests.
Your new class does not have to inherit from Activity to do its job. However, the onOptionsItemSelected() method may have to return some indication as to whether it actually handled the request or not so your Activity's method can call 'super.onOptionsItemSelected()' as necessary.

how to use Activity in sub class

Conceptual problem here!
I want to break down a program into simpler more focused classes. The first one I am doing requires a startActivityForResult() which in turn depends on an Activity.
I can pass the Activity to the class. But there is a problem in the caller when I use getActivity() this seems to be undefined for the class.
Why isn't getActivity documented by the way?

Android: Access method in an activity from another activity

My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.

Categories

Resources