Use single Fragment for multiple UI tasks? - android

I am using the Fragment master detail architecture
All Fragment Classes have the same logic
Is it possible to "detach" the Fragment from the layout and use just one Fragment class
So this code:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
if("001".equalsIgnoreCase(id)){
arguments.putString(Fragment001.ARG_ITEM_ID, id);
Fragment001 fragment = new Fragment001();
fragment.setArguments(arguments); fragManager.replace(R.id.item_detail_container, fragment);
}
else if("002".equalsIgnoreCase(id)){
arguments.putString(Fragment002.ARG_ITEM_ID, id);
Fragment002 fragment = new Fragment002();
fragment.setArguments(arguments);
fragManager.replace(R.id.item_detail_container, fragment);
}
fragManager.commit();
Will turn into Something like:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
GenericFragment fragment = new GenericFragment();
fragment.setUiId(id)
fragManager.replace(R.id.item_detail_container, fragment);
fragManager.commit();
List item

yes its possible, you can check and select which layout to use in onCreateView...
public static final String ARG_ITEM_ID1 = "fragment001";
public static final String ARG_ITEM_ID2 = "fragment002";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
String id = "fragment001";
if(ARG_ITEM_ID1.equalsIgnoreCase(id)){
rootView = inflater.inflate(R.layout.fragment_1_layout, container, false);
}
else {
rootView = inflater.inflate(R.layout.fragment_2_layout, container, false);
}
return rootView;
}

Related

Share information between fragments

I need to share information between fragments. I know that I can use sharedpreferences but I think that is not optimized.
This is the parent fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_blank, container, false);
....
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.eventos_pager, new BlankFragment2());
ft.commit();
....
return view;
}
This fragment call BlankFragment2 and BlankFragment1 need to sends information to BlankFragment2
I can share information to BlankFragment2 by the BlankFragment2's constructor. It is not allowed.
In Android you pass values to Fragments via bundles.
Using AndroidStudio File > New > Fragment > Fragment (Blank) will generate something like this.
public class MTFragment extends Fragment {
private String mParam1;
public static MTFragment newInstance(String param1) {
MTFragment fragment = new MTFragment();
Bundle args = new Bundle();
args.putString("value", param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("value");
}
}
}
From your code:
MTFragment mFragment = MTFragment.newInstance("Hello");
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.eventos_pager, mFragment);
ft.commit();

Fragment is called twice on screen rotation

I am new to android, and i am facing this problem when the screen orientation is changed. The fragment gets called twice whenever screen orientation changes. Below is the sample of my code. I checked other posts, but couldnt find answer. Anyone guide me through this.
public class SampleFragment extends Fragment {
static final String TAG_NAME = SampleFragment.class.getSimpleName();
List<PhrToolBar> mToolBarList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
DaggerHelper.getAppProviderComponent().inject(this);
mRootView = null;
getActivity().setTitle("Personal Health Records");
mRootView = inflater.inflate(R.layout.sample_phr_main_fragment, container, false);
mBinding = DataBindingUtil.bind(mRootView);
mBinding.setViewModel(mViewModel);
setHasOptionsMenu(true);
return mRootView;
}
Simple add this code
if (savedInstanceState == null) {
// only create fragment if activity is started for the first time
mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else {
// do nothing - fragment is recreated automatically
}

How can I pass Data from one fragment to another one? [duplicate]

This question already has answers here:
Passing data from one fragment to another
(4 answers)
Closed 6 years ago.
Hello i am working with fragment to pass data from one fragment to another with My Model object like Student with (id,name),but i cant able to pass and view data in second fragment from pass first fragment.
use static variable
if you use view pager then update fragment when a fragment is change
In Fragment transaction send data in constructor when when a fragment is replace
You can pass data from one fragment to other using constructor or using setArgument
Using Constructor
ModelStudent student = new ModelStudent(1, "ABCD");
FragmentTwo fragmentTwo = new FragmentTwo(student);
Using setArgument
FragmentTwo fragmentTwo = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putSerializable("STUDENT", student);
fragmentTwo.setArguments(bundle);
Fragment 1
public class FragmentOne extends Fragment {
private View rootView;
private Button btnPassData;
public FragmentOne() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.frgament_one, container, false);
btnPassData = (Button) rootView.findViewById(R.id.btnPassData);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ModelStudent student = new ModelStudent(1, "ABCD");
// first way
//FragmentTwo fragmentTwo = new FragmentTwo(student);
// or second way
FragmentTwo fragmentTwo = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putSerializable("STUDENT", student);
fragmentTwo.setArguments(bundle);
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragmentTwo).commit();
}
});
return rootView;
}
}
Fragment 2
public class FragmentTwo extends Fragment {
private View rootView;
private ModelStudent modelStudent;
private TextView txtStudent;
public FragmentTwo() {
}
public FragmentTwo(ModelStudent modelStudent) {
this.modelStudent = modelStudent;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.frgament_two, container, false);
txtStudent = (TextView) rootView.findViewById(R.id.txtStudent);
Bundle bundle = getArguments();
ModelStudent student = (ModelStudent) bundle.getSerializable("STUDENT");
txtStudent.setText("RollNo: " + student.getRollNo() + " Name: " + student.getName());
return rootView;
}

sending data from activity to fragment

I have a side menu, when the side menu is pressed. It should take the SharedPreferences String and send it to the Fragment. And the Fragment should set the TextView and it will be displayed on the MainActivity.
The sent data to fragment is not showing up in the MainActivity,I am getting the error. Any solutions?
Main Activity code (when option pressed):-
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
F1.setTextV(username);
FT.commit();
}
Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
return v;
}
public void setTextV(String username)
{
textV.setText(username);
}
Following issue is in current implementation :
1. If fragment_one is in layout which return in from onCreateView then use v to call findViewById :
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
return v;
2. Call setTextV after FT.commit(); :
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
F1.setTextV(username);
Because you are getting value from SharedPreferences to show in TextView so get value from Preferences in onCreateView and show in TextView.
In this particular case, you don't need to send data to the fragment because in a fragment you can access to SharedPreferences.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
SharedPreferences sp = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
textV.setText(username);
return v;
}
For other cases, the best way to send information from the Activity to the Fragment is using a Bundle.
if(num == 0)
{
SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
username = sp.getString("username", "DEFAULT");
FragmentOne F1 = new FragmentOne();
Bundle bundle = new Bundle();
bundle.putString("username", username);
// You can add all the information that you need in the same bundle
F1.setArguments(bundle);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.relative_main, F1);
FT.commit();
}
Fragment:-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
View v = inflater.inflate(R.layout.fragment_one, container, false);
textV.setText(getArguments().getString("username", ""));
return v;
}

How to Do Transaction of framents from one to another

I want to transact from one fragment to another... below given is my snippet...
I want transaction on button click...
public class Main extends Fragment {
// final View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
Button camera=(Button)rootView.findViewById(R.id.button1);
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.layout.main, new CameraActivity());
ft.commit();
}
});
return rootView;
}
so please Assist me in solving this...
[1]<--->[2]<--->[3]<--->[4]<--->[5]
where []=fragments...
[1]- has buttons a,b,c,d,e
How to move from 1 to 3 OnClick of c ...
Here is the Camera Activity
public class CameraActivity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.camera, container, false);
return rootView;
}
}
first of all this cant be activity
ft.replace(R.layout.main, new CameraActivity());
it has to be fragment
put something like this in your onclick listener in FirstFragment:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
SecondFragment fragment = new SecondFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
than in your SecondFragment onclick listener:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
ThirdFragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
and so on ...
method addToBackStack(null) is providing you back as simple as that. You could also pass some tag in that method if you need it, but I think from question you want be needing it.
Also if you need to start Activity than dont use this method, use Intent instead.
hope it helps

Categories

Resources