communicate between 2 classes in android - java

ok I have 2 classes
an activity class and another class which extended from maps.Overlay. so the second class cannot be extended from first class because it already extended.
my second class gets the geo coordinates from user touch location on a map. what I want to do is show a window(not alert box) to proceed. In order to do that, I have to alert the activity class somehow. because I can't call setContentView(R.layout.insertwin); in my second class. somehow it must need to call in the activity class. how would I do that?

You can do your own listener interface in the View subclass and subscribe to this listener int the Activity subclass after calling setContentView(). So you'll be able to notify your activity about any events you want.

You can implement singleton to do such things

Related

How do create a "manager class" with events for android?

Thank you, for reading this ;)
I have a class "scanner" with events that trigger when a barcode gets scanned and three activities. I want to let the user switch from activity to activity, but when an event is triggered in "scanner" i want the "scanner" class to start/resume a new activity (one of the three).
So, basically i want to create a "manager class" which events will always trigger, even if another activity is on the foreground.
How do i do this? I am coming from Xamarin Android so my Java knowledge isn't that great...
Java equivalent for event is an Observer design Pattern. If you look at the button Click handler you'll see listener interface. Listener is an Observer of the event.
If you want to handle navigation from any activity you need to have single navigation manager shared by activities. You can create singleton in Application inherited class. See how this is achieved. And subscribe to scanner events using observer pattern. Then since you need to get access to navigation manager create base activity class and implement there Navigation Manager member and navigation methods. Inherit from this base activity your's 3 activities.

Do all Android classes extend Activity?

I'm going through some Android development tutorials, and I notice that some classes extend Activity, and others extend ListActivity for example.
By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?
Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?
I read a few similar questions but it wasn't quite clear.
By "default", all Android Activities extend Activity.
By default, does a newly created class implicitly extend Activity or do you need to explicitly extend it?
You have to explicitly state that it should extend Activity if it does not already and if you plan to use it as an Activity. Kindly note that you there are times you wouldn't do it. For example, when you have a class for your objects or whatnot, then you don't need to extend Activity any longer.
Also, when a class extends ListActivity, why do you not have to extend Activity? Or does ListActivity already extend Activity?
ListActivity, AppCompatActivity, and any other class that ends in -Activity are "children of the Activity class. So by extension, extending by these "children" would already extend from the parent Activity class and you'll no longer need to do something like YourActivity extends ListActivity, Activity. I think Android Studio will give you an error if you do.
The difference between an Activity and a ListAcivity is that a ListActivity will require you to override several methods because it is expecting a ListView in its content.
Android is written in Java, and like all java classes, every class inherits from the Object class.
Does a newly created class implicitly extend Activity or do you need to explicitly extend it?
You need to extend it.
Does ListActivity already extend Activity?
Yes.
You can see class hierarchy for ListActivity here.
Newly created classes do not extend anything. ListActivity is a subclass of Activity so all of the functionality Activity has, ListActivity inherits. In the documentation for ListActivity you can see that it is a subclass of Activity.
In Android, All the classes are not extended by Activity.
Only the screens which render the UI are extended by Activity. So, if you want to create a UI screen then you have to explicitly extend it with Activity class and override the callback methods.
ListActivity is subclass of Activity which is only used when you have only a ListView in the your layout.It has some specific listview related methods.
Similarly, if you wanna create a fragment, your class will extend the Fragment Class and override its methods. Similarly for Service, BroadcastReceiver etc.

How to share a MediaPlayer object between multiple fragments?

I have set up 1 Main Activity (that extends Activity class) and 2 Fragments (that extends the fragment class).
I've tried setting up an Interface, which is implemented by the 2 fragments. And each fragment implements the particular function from the interface like this:
public stopMusic()
{
mediaplay.release(); // here, the mediaplay object belongs to only the respective fragment
}
Now, I know doesn't work, because the MediaPlayer object is not common to both fragments, i.e it's not being shared among them.
I'm looking to release the mediaplayer object streaming a file in Fragment1.java, if I hit a button from another fragment, like Fragment2java. Likewise, to release the mediaplayer object streaming a file in Fragment2.java, if I hit a button from Fragment1.java.
How can I make this happen? An example code would really help.
Let's say fragment A is the controlling fragment and fragment B is the media player. All communication should be done via the parent Activity. So in fragment B you create 'public interface(s)' which the parent activity implements, then the parent Activity should call the method(s) in fragment B.
Also depending on what you are really doing with the media player or whatever, does that belong in the background as opposed to fragment B?
Note: fragments should be loosely coupled and never communicate from fragment to fragment, always communicate via the parent Activity.
Hope this helps.

android -what approach to be used for some java code that repeats in many activities

I have an app that has a top menu as shown in the fig below. This is almost constantly used in all activities. For layout I have defined it in a single xml file and use <include>to include it in every layout file.I want to know if there is a way in java coding to avoid declaring all the imageButtons, and then using findViewById and onclick events for them , in each activity. The top four icons will act as menu and be available in all layouts and activities
First of all, what you are trying to achieve is against the android standards and this could affect the user experience for Android users. Instead, add the menu items on the action bar.
Anyway, you can achieve what you are looking for by defining a base class (named it like ActivityBase) and do all the initializations and listeners registrations on it. Then, extend from that base class. Bear in mind that each activity will have its own instance of the views of the base class and the state will differ from activity to another.
Although you have accepted an answer I disagree that the Application class should be used to host global methods. It can be used for maintaining global state but that's a different matter.
As others have said, use inheritance by creating a base Activity class then extend that for all of your other Activities. To make things easier, however, you can define the onClick method to be used for each of your buttons in the layout file itself by using (for example)...
android:onClick="myOnClickMethod"
As long as the base Activity defines a public method called myOnClickMethod (or whatever method name you choose) with a void return and which accepts a View parameter it will work without the need to implement View.OnClickListener and without having to set the listener in Java code. Example
public class MyBaseActivity extends Activity {
public void myOnClickMethod(View v) {
// Get the resource id of v and use switch / case to perform action needed
}
}
That's all you need.
Write it only in the first activity. Extend it to the other activities instead of extending with android.app.Activity.
Eg:
public class SecondActivity extends MainActivity{
}
Put that method in MyApplication class which extends Application. So, that it can be accessible by multiple activities.

Detecting when activity changes

I am trying to add a save dialog to an Activity.
If you press back, I already have captured it and added a save dialog box.
As I have a generic action bar, is there a way to capture the change of activity event without hardcoding it into the activity change itself?
I Googled it a bit and found no luck, onDestroy and onStop don't seem to do what I want.
If you want to know whenever the Activity ceases to become visible (for whatever reason), override onPause (see http://developer.android.com/reference/android/app/Activity.html).
As an aside, as a user, I would want this behaviour - if I move away from an Activity without pressing the back button or quit, I am hoping that it will handle everything silently - ie you use onPause (or alternatives) to store things so that when the activity resumes it has everything as I left it.
One way you could do this would be write a class which inherits from Activity and implement the behavior in that class. Then simply inherit this class for all your activities.
You could write one class which implements the save behaviour
class SaveActivity extends Activity {
//...
#Override
public void onBackPressed() {
// common behaviour you want
}
}
Then in your activities you could do this
class MyActivity extends SaveActivity {
// code for this activity
}
onPause() might be what you should be looking at. It is called when any activity comes onto the top of this Activity.

Categories

Resources