The UI elements in the MainActivity class need to be preserved and the other class file adds a new UI element to the main xml layout that's used by both classes to differing degrees.
It's understood that you can create a base abstract class and two concrete inherited classes however in this case there a third supporting class for the second (other class) and it requires a handler to function.
For perspective, a button (in activity_main) is clicked and it should launch an activity while maintaining the UI elements used MainActivity. Furthermore the button has it's own class file methods and isn't in MainActivity.
What happens now? The button is pressed and nothing happens. Manifest confirmed so its not that. Or I allow the main activity or the other activity and it works, both need to work simultaneously.
Basically MainActivity needs to act as the base abstract activity for the separate class file.
You are messing up activities and views. To reuse the same business logic, you can write common logic in base class for all other activities (i.e. class BaseActivity extends Activity). To reuse different UI parts you should either use fragments or you can use <include>/<merge> tags pair to include a certain layout into another layout.
Related
I'm not PRO in JAVA , but I found myself of getting crazy with Android activities getting too large.
After a few pages of code length I found myself of permanently scrolling back and forth!
Not only OnClickhandlers can grow huge, just a lot of code tends to sum up in a bigger activity.
Fragments were no solution for me.
In general Java does not allow to split up a class into several files so a hack is needed.
I browsed through a lot of stackoverflow threads regarding that, not one had a solution.
I solved it anyway, maybe this helps others.
This is a bit Android focused as I got anoyed by my huge onClick Handlers for all the menus and buttons but I guess it will work for other general Java problems as well the same way.
A simple approach that I follow is to move the View concerns into a separate class (let's call it ViewManager) and make the Activity/Fragment work only as a controller.
A ViewManager is generally responsible for -
Inflating the layout
Getting references to all the views
Displaying data in the views
Handling click/touch events
Animations
The Activity/Fragment is only responsible for -
Fetching data and passing it to the ViewManager to display it
Handling navigation
Posting data to a server/DB
For UI controls that trigger an action that the controller is responsible for, say launching a new activity when a button is clicked, the ViewManager receives the click and calls a method in the controller that takes care of the navigation.
If you want to further eliminate boilerplate code (click handlers, findViewById() calls, etc), consider using libraries like ButterKnife.
One solution is simple, you can make the main class fields public
However that will mess up your code, you should keep the fields private when possible.
Aside from better code completion of your IDE it's also protecting the classes from illegal outside manipulation.
Now the trick for me was inner classes.
An inner class can access the private fields of it's parent, however Java does also not allow to put an inner class into another file.
It has to be defined INSIDE the parent class.
This is where I started to dig and found a solution which might be considered to be acceptible.
If this hurts java experts, I'd appreciate not to be downvoted ;)
The trick is to create an "abstract class" in an own java file.
I named it a bit outside conventions to make it stand out: InnerMainActivity_onClickHandlers.java
MainActivity is my main class (the parent of the new inner class)
In my Parentclass I have this field defined, a normal inner class but extending the new class:
private class inner extends InnerMainActivity_onClickHandlers{
public inner(MainActivity mainActivity)
{
super(mainActivity);
}
};
In my MainActivity.onCreate:
Button.setOnClickListener(new inner(this));
As you can see, as you can see it passes the MainActivity (Parent) to the Inner class.
Now the Inner class iteself:
public abstract class InnerMainActivity_onClickHandlers implements View.OnClickListener
{
private final MainActivity main;
public InnerMainActivity_onClickHandlers(MainActivity mainActivity)
{
this.main = mainActivity;
}
#Override
public void onClick(View view)
{
// here implement the listener
}
}
To make this work as advertised you need to change the private properties of your MainActivity/Parent to protected.
Now the extended inner class has access rights to the parent fields, however any external class can still not access it.
In this case I have a dedicated class for all onclick listeners, a second class could be used for other things.
When is Activity.onBackPressed called in my Android application? I'm not looking for the obvious answer of when the user presses the back button. I wan't the answer in relation to other "callback" functions.
Is is possible to be called during the execution of another function within the Activity class?
What is the case if I have my Activity class implement some typical interfaces used for your typical game? For example GLSurfaceView.Rendered? I'm having the feeling onBackPressed is called during GlSurfaceView.Renderer.onDrawFrame but I'm not 100 % sure yet. Even if this isn't the case, I want to know how it works. (It seems difficult to find this kind of simple information anywhere.)
Finally, below is a code example for the layout of my Activity class. The question is, however, not limited to this particular setup.
class MainActivity extends Activity implements Renderer {
onCreate(...) {
layout = new FrameLayout(this);
GLSurfaceHolder glsurface = new GLSurfaceHolder(this, this);
glsurface.setRenderer(this);
layout.addView(glsurface);
setContentView(layout);
GLSurfaceHolder is just a simple dummy class that extends GLSurfaceView. It has the onTouchEvent overloaded and simple passes the data over to the MainActivity class. (The design philosophy in this very, very simple app is just to focus all the sensory and other data to one place and then "make things happen"..)
onbackpressed will be called when you pressed back button. Default behaviour will be destroying the activity. To avoid override the onbackkeypressed or onkeypressed.
To use fragments in an activity, we need to extend Fragment in that activity. But my activity already extends another class, and java cannot extend 2 classes. So how to use fragments inside an activity that already extends another class?
Multiple inheritance (more than one parent class) was tried in C++ and gave some interesting challenges, so the Java designers decided to try "interfaces" instead.
For your actual problem, have a Fragment object inside your own object and invoke the methods you need.
I have a couple of things which are the same in all my Activities throughout my application, e.g. an optionsmenu and some code which needs to run onresume, onrestart and onpause. I figured it would be a smart approach to put them in my a class MyListActivity extends ListActivity and then have all my activities extending MyListActivity.
This worked out just fine until I created an activity which didn't have a ListView. the App crashes because ListActivity expects a ListView. However, this new activity does not need a ListView, but would still need all my functions / Overrides in MyListActivity .
Right now I can think of two solutions. One: add a dummy listview to the layout with visibility = false, height & width = 0 (haven't tried this, but i guess it should work). And Two: copy/paste the contents of the MyListActivity class into a MyActivity extends Activity class. I feel very silly doing this, but I don't have any other ideas on how to solve this issue.
Any ideas on how to handle this nicer?
Thanks
I think you can implement all the features in an Activity subclass (e.g., MyActivity) and make the MyListActivity class a subclass of the MyActivity class.
Other approach is to make a helper class which contains all the features in static methods with an Activity object as the first argument. In this case you don't need to create MyActivity or MyListActivity classes, but you need to call methods of the helper class in every Activity subclass you want to inherit these features.
i'm a junior programmer. I have a base class that extends activity, i use this class to set up the menu in my app. All my activity classes extends this base class. Now, i have a new class which extend ExpandableListActivity this class's purpose is to show an expandable list and of course when the user is viewing this activity the menu becomes unavailable. So how can i do to be able to use the menu while viewing the expandable list. I know that it's impossible to extend more than one class in Java, so what's the trick?
Thanks in advance
You can use delegation. Extract all methods related to Menu in separate MenuDelegate class.
Your new class will extend ExpandableListActivity, and delegate all methods related with Menu to MenuDelegate. It's useful solution if your Menu logic is simple.
In this case, your other activities will be free, and will be able to extend ListActivity, TabActivity or something else..
http://en.wikipedia.org/wiki/Delegation_pattern
You don't have to extend ExpandableListActivity to have an ExpandableList in your layout. You can extend your BaseActivity and handle the ExpandableList on your own. The ExpandableListActivity is just a helper class.