Android wizard-like (step by step) application - android

I'd like to build an android step application so I can go back and forth between activities.
I'd like to create a base class for the step manager that includes two buttons to go forward and back, so in the child activities I don't have to recreate those controls every time. The step manager class has to take care of the switching between activities in a precise order and of passing data between them.
I tried to write some code but I can't merge the parent's layout with the child one!
FrameLayout fl = (FrameLayout)this.findViewById(R.id.frame);
LayoutInflater.from(this).inflate(rId, fl, true);
This code is in the parent class (inherited from Activity):
the FrameLayout is the place holder for the child-layout controls;
rId is the layout resource ID of a LinearLayout in the child xml layout.
I guess that the problem about that code is that I don't call the setContentView of the child layout, so anytime I use the findViewById of a child control I get a null pointer.
What is the best practice to create something like that?
Thanks, Simone.

You can use a single activity thus avoiding passing data between them and saving memory.
In this activity you can use a ViewSwitcher or (better) a ViewFlipper to let the user navigate between steps.

Related

Why should I not add Fragments to the activity layout XML file?

Edit: source : http://developer.android.com/training/basics/fragments/creating.html last parargraph says :
Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson.
Thanks in advance.
You can use fragments in two ways ,
Static Fragments
Here you can define the fragment in whatever the layout file you need. Only thing is, that defined fragment can not be change at the run time. So, re-usability will be issue here, you cant take the advantage of the re-usability of fragments in this case.
Dynamic Fragments
Here you can define a place holder(frame layout etc) on your layout and you can add/replace whatever the fragment you expect at any time while your activity is running. This ensure the re-usability.
Also you can use backStack if back navigation is required.
So, it depend on your requirement.
If the fragments are defined in Xml you won't be able to change them at runtime; in that case I'd suggest using an <include layout="#layout/my_inner_layout"/> tag, that is not only better performance-wise as it is easier to use (you can access the views defined in my_inner_layout.xml using findViewById() method from the host activity).

Android Change Different Layout

I have a TabHost set up with tabcontent. However, this tabcontent doesn't take up the entire screen and a different layout is loaded at the top. How can I change the image in the layout above from within the on create of a tab activity?
Thanks,
You will not be able to access Activity A's view from Activity B; this is because it's very possible that the view will be destroyed when you launch Activity B.
What you can do however is somehow let Activity A know to change the ImageView when you come back. For example, you can have a static field or you can use setResult().
I see Three ways:
1.Share the ImageView between two activities. To do that, you can put this element as private and create a getter. Then, you just have to access this element and change the element. I'm not sure that will work.
2.preferred way
Load your image in onResume from a remote source. Then, you juste have to change this remote path from your second activity.
3.previous response

Creating a copy of a View?

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 .

passing objects to another activity

Situation
In my alarm clock app I start a NewAlarmActivity by clicking a button.
After the user has made all the selections, alarm gets set and `finish() is called.
The matter
I create a new LinearLayout object with images and text within it. That layout must be added to the screen of previous activity (to another LinearLayout placed inside a ScrollView) so the user is able to see the alarm set. Somehow I have to pass that LinearLayout object to the first activity and tell it to receive and add the object to the screen.
How can I do that?
You just need to read some documentation about startActivityForResult().
BTW, IMHO, you should not transport your LinearLayout object between activities, that's ugly.
How to manage `startActivityForResult` on Android?
Starting Activity And Getting Result
This has been asked countless times...
No You don't have to do this. Try the approach of storing your data into a database and then when going into that Activity, build your layout according to the data you have. This is a much better way than passing a layout from one Activity to antoher.

Dynamic adding of multiple fragments

I am thinking of an app in which I will be programatically able to display some fragments according to metadata I have stored somewhere. Up to know, I have been able to find out, that each fragments lies in corresponding FrameLayout, or especially, when I create activity with one FrameLayout, I am able to store there only one Fragment at a time, no matter what kind is it. Problem is, however, with situation, when my metadata declares I have to put 3 fragments into my activity, while there is only one FrameLayout.
I see two possible solutions:
1) making several FrameLayout and in final stage, some of them will be used or not
2) somehow join multiple fragments to fit into one available FrameLayout
I don't like solutuion 1) and I don't know how to achieve 2). How to you see it? Is it possible to dynamically add multiple Fragments into activity with one Frame Layout?
In your situation where you require more number of Fragments to be added to your screen avoid using the FrameLayout, you will not be able to achive it. There are several factors that define how you layout multiple Fragments
Are all the fragments of the same size?
Should the fragments be place in a particular order?
First Create an empty parent layout (Layout class is left to your choice except FrameLayout). Next define your child view for your parent layout using a seperate xml file this file must contain your fragment place holder. Using the inflator inflate your child view and assign a Fragment to the inflated layout and eventually add it to your parent layout, through this way you can active what you are looking for. Just remember as you inflate your layout do mention its layout size so that you achive the kindly of layout you want.

Categories

Resources