How can I access a view of an activity from another activity - android

I need to hide the tabs of a TabHost from a child activity. I tried getParent() and it didnt work
Also tried :
TabHost th = (TabHost) ((TabsActivity)getBaseContext()).findViewById(android.R.id.tabhost);
th.setVisibility(View.INVISIBLE);
But it throws a nullpointer

Never directly access Views owned by other Activities from the current Activity, because they might have been recycled in the meantime (because the other Activity might have got destroyed in the meantime).
You need to communicate with the other Activity in a standard way. For example, your child Activity can return a result to the parent Activity, which it can then interpret (you can put the value to the Intent). Another solution is using a static variable, but this is not too nice and has risks if you're not careful enough.
For more information about communication between Activities, see this and this.

You won't be able to directly manipulate the view hierarchy of an Activity not currently in the foreground because it likely would have been paused and stopped, in which case its UI has already been destroyed and won't be rebuilt until it comes back into the foreground. You should consider what things take place in the Activity lifecycle to help you solve this. A quick and dirty solution could be that your Activity hosting the tabs has a public static variable that can be set by other Activities; such that when it resumes running in the foreground it can check that variable and make Views visible or not as needed

Related

Why do we need startActivityForResult() method instead of just setting Parent's public variables?

Since the child activity is created by calling the startActivity(intent) from the Parent Activity, it is guaranteed that the parent object exists while the child activity is running.
From that point of view, can I just manipulate the Parent's public variables at the child's class instead of calling startActivityForResult() after I come back?
Why do we need to use the method?
it is guaranteed that the parent object exists while the child activity is running.
No, it isn't. For starters, startActivityForResult() can be used across process boundaries, where the activity you are starting is in another app. The activity that is started cannot access the activity that started it, as they are in separate processes. Beyond that, it is possible that the process where the original activity resides will be terminated while it is in the background, as the user is in the second activity — this happens a fair bit when the second activity is a camera app, launched in response to something like an ACTION_IMAGE_CAPTURE Intent.
can I just manipulate the Parent's public variables
One activity has no access to another activity instance. The only way that would be possible is via static references to activities, which are tricky to get right without introducing memory leaks. And, again, that would only work where both activities are in the same process.
Why do we need to use the method?
You don't. There are any number of ways of writing UIs on Android. Few require the use of startActivityForResult(). For example, if these two bits of UI are that closely coupled, perhaps they should be in a single activity, where the bits of UI are separate fragments.

Call function in parent Activity from child Activity - Android

How can I call function (with parameters) in parent activity from child activity in Android. I iOS is simple with delegate and protocols, but in Android I found that can be a problem if the screen is rotated.
I also do not want to use startActivityForResult because I do not want to close the child Activity.
ADDED:
I am creating a library so other developers can attach it to their project. In developers activity they put button that shows my(by my I mean from library) Activity and when something is done in my activity I instantly need to inform the main project(activity) that something happened (via function or something), but I do not want to close my activity yet. I also should not change their code a lot and it should be easy to include to their code.
The point is that once you started a child activity, Android might decide to close your parent activity at any time, if system requires more memory. This means you cannot rely on the fact your parent activity still runs. That is why you cannot use static variable approach either, because parent activity might not be there already.
If you want your app to run reliable in any situation you have to use startActivityForResult and close child activity.
If this is not acceptable for you because of some reasons, you might share more details and we will try to find an appropriate solution for you in terms of Android concepts.
ADDED: I am creating a library...
As already mentioned, you cannot control whether your parent activity gets closed or not as long you started a new child activity. Android can kill your parent activity at any time and you need to be prepared to handle this situation properly.
To solve your problem you need a place, which is shared between parent and child activities. A sequence will be like this. Parent activity starts a child activity and goes into background. User changes something in child activity and it stores changes (as data) into the shared place. At this step it doesn't matter whether parent activity still running or now. Once parent activity is visible again, if must read data stored by child activity, and update itself accordingly. You can use onStart() method for this.
Now about that shared place. I would discourage you from using a static variables. Instead you could use Application object (it's a singleton) or shared preferences (they are also shared).

Android Service > Activity > Fragment with ViewPager

First, I'd to state that I've been searching for a solution for this problem for three days now, that may means either I'm not asking the right question or not using a good approach. If any, please guide me in the right direction.
This is the scenario: I've an Activity and a bound Service. The Service holds some data and processes it as necessary while posting a persistent (ongoing) notification with some information. The Activity has three Fragments inside a ViewPager that displays the data processed by the Service.
The Fragments are a List Fragment, that shows the active data entries available, a Details Fragment that displays the details for each data and a Parameters Fragment where the user can modify how the data is processed.
[Service] <-> ([Activity] -> [ViewPager([List], [Details], [Parameters])])
Everything works just fine. My Activity binds to the Service, the ViewPager is created after and then the Fragments fetch information trough an Interface.
Here comes the fun part... Screen Rotation!
As the Service binds asynchronously, when the user rotates the screen the Fragments no longer have the data because the Activity is bounding the service while they're already present and not recreated thanks to the ViewPager.
I've been trying to figure this out but it seems that I don't have the knowledge to solve it. I've tried making static references to the fragments, setting them up before the service is rebound but I can't get a stable solution.
I'd be using android:configChanges in my manifest but there are different layouts for each orientation.
Again, if I'm using a bad approach, please, guide me!
Difficult to suggest when I don't know your code but thinking out loud....
Can you have a "worker fragment" that is never displayed (i.e headless) and has setRetainInstance(true) set so it does not lose any state you have set.
Your worker fragment would bind to the service instead of the activity and maintain a reference to it.
If you need to communicate with your Activity, you can do this with callbacks.
Your other fragments could communicate with the worker instead of the Activity.
This process would basically make the activity little more than a shell into which the rest of your components are hosted. Rotation would lose nothing because all data is held in the retained fragment.
During the screen rotation process the activity is completely destroyed and use of android:congfigChange is discouraged. but what you can do is you can override saveInstanceState(bundle) method in which you can save the data present in your activity at the time it is destroyed by the system in response to the screen rotation. and later receive it as the system passes the bundle to the activities onCreate(bundle) method or get it from the restoreInstanceState(Bundle) method.

Android - Getting activity object from startActivity

I need to get the instance of the Activity that was created when startActivity() is called. However, startActivity() returns a void value.
I guess startActivity() doesn't wait until the Activity is created.
Is there a different way for me to get hold of the activity that was just created on the call to startActivity? Or, perhaps I can create an Activity instance myself and register it somewhere so that startActivity can find it.
Is the Activity you're trying to look at in your own app, or in another app?
If they're both your Activities, then it might be OK to do what you're trying to do, although Activities ought to remain separated. You might consider using Fragments instead, since they can "communicate" with each other through their parent Activity.
If they're not both in your app, then you can't get the other Activity instance, because by definition it's in another process. Moreover, the Android system prevents it, because it would be a big security hole.
In general, I squint a bit at attempts to get an instance of something outside of your own component, unless it's an instance of something in the system. Communication between components should be by Intent, bound Service features, or content URIs. Passing around instances leaves you open to memory leaks.

How to make a core of activity on android?

I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.

Categories

Resources