How do you implement a fixed view through various activities in Android 2.1 upwards? By fixed I mean that the view should retain its state when the activity changes.
In particular, I'd like to have an Admob AdView on top of every activity without reloading the ad every time the app starts a new activity.
I don't think it is possible using different activities. But you could use only one activity and split your screens through multiple fragments inside this activity and adding/removing them while keeping your fixed view untouched.
This is not possible. The Activity is the basis of the app's user interface, and every View must live within the Activity's parent ViewGroup. Since every Activity must be created when it is initially launched, every View that lives inside the Activity must be inflated and attached to the Activitys layout.
What you could do instead is pass the information required to instantiate the specific admob View to the next Activity with an Intent.
Related
The design of the application that I'm working on is fairly simple so I've decided to use Activity.setContentView() to switch between the few different layouts it has. This all happens within a single Activity, of course.
To explain it better - let's say that I have a main layout with a simple navigation and a settings button.
The problem is that whenever I show the settings view upon click and later try to set the main view back, it is unusable. It just loads the main layout, but no listeners are set, as if I show a picture.
I've figured out that I should use the setContentView(View view) constructor instead of the one that passes the layout id from the resources. Though, I have no idea how to save the current view..
How to save the current view with all its listeners and stuff to then pass it to the constructor and save my data?
Is there a way in an Android Activity to do a setContentView(), so that I can have Android compute the layout, and so I can successfully get Views in it via findViewByID(), but not yet display it?
This would be in an app with a main activity and some subordinate activities and the one I want to start but not display would be one of the subordinate activities. (in other words the main view would already be filling up the screen, so it would be sufficient to simply keep the new one hidden at the bottom of the view hierarchy). The activity would be started with a "standard" launch mode.
If there's a way to do it by keeping it at the bottom of the View hierarchy, how would I force it to the top when I do want to display it?
NOTE: This app already exists - it's a large, complex industrial app with 14 Activities, written for Android 2.35 and 2.36, so re-architecting it to use Fragments instead of Activities would be impractical. I just want to modify one Activity to not display, or to just display at the bottom of the View hierarchy so it's not visible.
This would be in an app with a main activity and some subordinate activities and the one I want to start but not display would be one of the subordinate activities
That is not possible. Or, more accurately, you are welcome to start that activity and not populate its UI, but it will still take over the screen, and so the user will be presented with a blank screen, which is not especially useful.
in other words the main view would already be filling up the screen, so it would be sufficient to simply keep the new one hidden at the bottom of the view hierarchy
Each activity has its own view hierarchy. A "subordinate activity" cannot and will not be at the top, bottom, or anywhere else with respect to some other activity's view hierarchy.
Im quite new to Android but I think that this can bea easily done using fragments. One fragment will be the one that will be currently shown and the other will be overrlayed with a hiden parent view for an example. When you need to show the other fragment just set the layout to visible.
Hope it works, Good luck.
If you want to do this without using Fragments which I suggest you to use them you can use LayoutInflater to inflate whatever layout you like and change between them using setContentView repeatedly.
View layout1 = LayoutInflater.from(this).inflate(/*your first layout */ R.layout.activity_layout_1, getWindow().getDecorView());
//here you can use findViewByID like this
View someViewInLayout1 = layout1.findViewByID(R.id.some_view);
//... get fields for all others views you need here in layout1
//than you inflate your other layout
View layout2 = LayoutInflater.from(this).inflate(/*your second layout */ R.layout.activity_layout_2, getWindow().getDecorView());
//... do the same for layout2
Now you can call setContentView whenever you want to change between layouts.
setContentView(layout1 /*or layout2*/);
I have 2 different activities that share one layout.xml file. If I decide to make alterations such as TextView.setText() in one activity, would the other activity experience the same change? Or do the activities just create different instances of the same layout?
If you alter the view in one activity, the other won't be affected since you can only have one activity shown at a time so the other one will have to be recreated and redrawn on the screen.
I have 2 Activities: A,B.Layout of Activity A,has a viewgroup that user changes it's content.In Activity B,I have to show that viewgroup again,without any change,it must be a real copy of that viewgroup,so texts,colors,dimensions,order(of childs) and ... must be same.So I can not use Layout Inflater.Is it possible without creating classes of the type of childrens of that viewgroup and change properties?Because if I have more than 2 Activity with different viewgroups,it is very difficult to show viewgroups of each activity in last Activity.
Also I can not remove those viewgroups from their parents.
If their content will be the same, there is no point in having two different activities. You can dynamically change one activity' s content and the behavior will be the same as there were two activities. If it is really necessary, then you will have to save all the needed information to reconstruct the activity again and pass it to the newly created activity. Take a look at this.
No easy way to do this. You cant move view's between activities. So you have several options:
create a bitmap of viewGroup and show in in new activity (doesn't work is you need editable copy)
save view's hierarchi state in old activity and recreate it in new one (using fragments makes it easier).
do not create new activity. Just change some UI parts in old one not touching target ViewGroup.
Create a class that holds the configuration of your view group. Let this configuration class hold all the information related to your ViewGroup. It will hold the texts, the colors the dimensions the order and everything user has changed. Pass object of this class from Activity A to Activity B and using this, reproduce the same view by inflating the same layout.
Hope that helps.
And to answer your question, there is no other easy way of doing this.
For the ViewGroup that needs to be shared, refactor it into a Fragment named C. Then create Fragments for the sections of Activity A and B minus this shared portion. Then contain all of these Fragments within a new containing Activity (you will no longer need Activities A and B).
Fragment A and C will be the new Activity A. Fragments B and C will be the new Activity B. To transition from the first state to the second, do a FragmentTransaction adding Fragment B and removing Fragment A. Remember to add this transaction to the back stack so the back button will bring you back to the first state.
Just a general doubt ? why would you want to go for two activities why not use two fragments assign to the same view use the underlying activity to store all the changes happening in one of the fragment (View) and when the user is passing to the other view just send in the parameters to the second fragment . Thereby you are emulating to the user that it is two activities but in reality its just two fragments controlled by a single activity .
We use different activities to navigate through our app. One of them is very complex and contains a lot of nested views/images etc, so when I usestartActivity(intent1) in the activity before it, there is a short delay and it feels/looks laggy.
All the information needed to create the content views is known in advance.
So my question is: is there a smart way to prerender/preload the activity or its content view?
As i figured the intend only holds information about the next activity but no instance of the activity itself, so i assume there is no way to tell the intend to create the activity before i call the startMethod.
One idea i hat was to create a static view before starting the activity and set this view as contentView in the onCreate() method. But it seems like a bad hack to me.
Thanks in advance!
The best solution would be not to start a completely new activity but using a ViewPager or ViewFlipper. Switching between Views should be then nearly instantaneous and you also get the chance to easily apply animations.
If that is not possible you could start a new activity but put a ViewSwitcher in there. The first View would be a progress bar. The second view is inflated and added to the Switcher in a background thread.