I have a button in an Activity and when on clicking this button, some operation will be performed in another activity. And I should call the buttonclick event in 2nd activity only.Simple saying, I have a TabActivity with button and upon clicking the button some operation should be performed in the underlying tabs. ButtonClick event should be in the tab.
How can I achieve this?
create one common function in a helper class and call it from both the places.
You could broadcast an Intent from your first activity to be received by your second activity. When your button is clicked, it's parent activity broadcasts a unique intent which your android manifest will route to your second activity for receiving.
There is a good example of that here:
http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
However, have a look at the android reference and consider using a LocalBroadcastManager which is more appropriate if you are just broadcasting within your process.
Related
I want to call the click event of specific button of other activity from my new activity. Is it a possible in android and if yes then how can I achieve the same.
You could implement and interface in the one activity and call it in the second activity.
I have a condition when a specific event will occur in background, I have to show a Floating Action Button in bottom of activity to notify the user if App in fore ground. No matter in what activity the user is, if that event trigger by a service, I have to show a FAB not a dialog, I mean user can interact with activity and FAB both at the same time. How could I achieve this if I do not wanna repeat the same code in all activities of my app.
You can make a utill class and make a static method in that class so in which activity you want to access that method just Classname.methodname and send parameter.
or you can use that method in that Activity and extend from that activity
You can create a simple activity that matches your need and make all other activities derived (inherited) from that one.
Is there any way to change the parent of an activity from class file.
I have two activities say ONE and TWO, and both of them can start a new activity say THREE.
Now if ONE starts THREE, then for up Navigation parent should be ONE.
But if TWO starts THREE, then for up Navigation parent should be TWO.
If i can change the parent for an activity from my class file.
I will send some data from activities (ONE and TWO) to uniquely identify them then set the parent according to that data.
If there is any other way to do this then let me know.
I think you can only have one defined parent in your manifest but you could just send your parent when you launch your activity. Just pass a bundle with what activity launched your Third activity and handle it in OnBackPressed and if you want to change your up button handle it in onOptionsItemSelected.
In android activities are added in stack as they are started. Having said that, when you start activity THREE from activity TWO, activity TWO is added in stack below THREE. Now on click of up button in activity THREE, you can finish activity THREE and the control will automatically return to activity TWO.
Please follow this post for getting up button press in android. Now in its onClick case, you just need to finish activity THREE, and the control will return to whichever activity you came from. Whether it be ONE or TWO. You can finish the activity in onClick as following:-
Just write
finish();
in the case for home button
I wanna know whats the best way for moving through activites. What happens if i call intent from child activity to go back in parent activity? Do activities accumulate in memory?
To be precise, in child activity i have a button for sending sms message. I want to go to previous activity immediately after the message is sent (or button is pressed), because I want to stop user from pressing send button again.
If you want to go to the previous activity, simply call finish(). That will return to the last activity on the stack. That's usually the activity that invoked this subactivity, unless special flags are used in the intent.
Android activity should be stored in a memory stack.
After the button is clicked in child activity, if you call function finish(), it should return to the parent activity.
in my app i am having an Activity that launches another Activity ontop of it. The second Activity is meant to provide controls like next and previous for the first activity.
How can i pass button events between the two activities that are visible at the same time without closing the Activity with the controls?
You can send Broadcast in order to communicate between Activities.
In parent Activity register BroadcastReceiver, then send a Broadcast by calling sendBroadcast(..) in child Activity.
It sounds like what you are trying to do doesn't require two activies, but rather additional view objects on the main activity that will be shown/hidden/removed/added as buttons on this activity are pressed.
If you need additional help, I would recommend elaborating on your original post.