Android interview question for the developer
Hi Guys, in the previous post we see the interview question for android In this articles also see remaining Android interview question for an experienced developer with the answer, by reading this article you can easily face interview, let’s start from fragment related questions
1. How to open a fragment from the activity
We can add or replace fragment from activity, let’s see one by one
1. Add:
You can add fragment on the container to do this, firstly you need to specify layout resource id like R.id.layout_container so that FragmentTransaction can add fragment on that container.
Add FrameLayout in your MainActivity.xml we will use this as a container to add or replace fragments.
<FrameLayout android:id="@+id/layout_container" android:layout_width="match_parent" android:layout_height="match_parent" />
This is how you can add fragment on layout_container
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.layout_container,BlankFragment.newInstance(),""); transaction.addToBackStack(null); transaction.commit();
2. Replace:
Replace fragment is the same as remove(fragment) from all currently added fragment and add a new one on same container view id
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.layout_container,BlankFragment.newInstance(),""); transaction.addToBackStack(null); transaction.commit();
3. addToBackStack:
This is used to add this transaction to the back stack, which will be remembered after it committed.
2. How to open a fragment from another fragment
To open fragment from another fragment you have to replace the fragment
BlankFragment2 nextFrag= new BlankFragment2(); FragmentManager fragmentManager=getActivity().getFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.layout_container,nextFrag,"tag"); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();
How to pass data from one fragment to another fragment?
You can pass a bundle as an argument like below:
BlankFragment2 nextFrag= new BlankFragment2(); Bundle args = new Bundle(); args.putString("YourKey", "YourValue"); nextFrag.setArguments(args); FragmentManager fragmentManager=getActivity().getFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.layout_container,nextFrag,"tag"); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();
In onCreateView you can get value like below:
String value = getArguments().getString("YourKey");
3. What is ConstraintLayout
Constraint layout gives you adaptable and flexible ways to create a complex layout using views for your apps. it is now default layout in Android studio
Features of Constraint Layout
- Convert from other types of layouts to Constraint Layout.
- You can place dynamically position UI elements onscreen in relation to other elements.
- You can Animate your views.
4. How to use weight in Layout
You have to add android:layout_weight=”1″ to child layout android assign sum of the weight of child layout to parent layout android:weightSum=”6″ as given below:
Note:- make child layout width is 0 android:layout_width=”0dp”
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="6"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> </LinearLayout>
Output:-
Check more question answers in this post
Thank you 🙂
Hey there, You’ve done an excellent job. I抣l definitely digg it and in my opinion recommend to my friends. I am sure they will be benefited from this site.
Thanks a lot 🙂