Pass data between fragments - android

I have one MainActivity and two fragments. In FragmentA I have a recycler view. If I click on "add" button there, the FragmentB is open. The thing I would like to is to write text into some EditTexts and send data back to FragmentA (and render that data in the recycler view). Could you suggest me something please? Thanks
FragmentB
public class NewContactFragment extends Fragment {
EditText name, number, email;
public String mName;
public String mNumber;
public String mEmail;
boolean isFavourite = false;
public NewContactFragment() {
// Required empty public constructor
}
public static NewContactFragment newInstance() {
NewContactFragment fragment = new NewContactFragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
//set title
((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.new_contact);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_contact, container, false);
name = (EditText) view.findViewById(R.id.ed_name);
number = (EditText) view.findViewById(R.id.ed_number);
email = (EditText) view.findViewById(R.id.ed_email);
mName = name.getText().toString();
mNumber = number.getText().toString();
mEmail = email.getText().toString();
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.new_contact_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
//TODO: save editTexts and return to ContactListFragment
break;
case R.id.action_favourite:
getActivity().invalidateOptionsMenu();
//Toast.makeText(getContext(), "isFavourite is: " + isFavourite, Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
FragmentA
public class ContactListFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "newcontact";
FloatingActionButton fabButton;
SearchView searchView;
RecyclerView recyclerView;
ContactsAdapter contactsAdapter;
List<Contact> mContact = new ArrayList<>();
public static ContactListFragment newInstance() {
Bundle args = new Bundle();
ContactListFragment fragment = new ContactListFragment();
fragment.setArguments(args);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact_list, container, false);
searchView = (SearchView) view.findViewById(R.id.search_view);
fabButton = (FloatingActionButton) view.findViewById(R.id.fab_button);
fabButton.setOnClickListener(this);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mContact = SugarRecord.listAll(Contact.class);
contactsAdapter = new ContactsAdapter(getActivity(), mContact);
recyclerView.setAdapter(contactsAdapter);
inputFilter();
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
//show actionBar
((MainActivity) getActivity()).getSupportActionBar().show();
//show title
((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.app_name);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
public void inputFilter() {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
contactsAdapter.filterList(newText);
return true;
}
});
}
#Override
//Fab button listener
public void onClick(View v) {
((MainActivity) getActivity()).showFragment(new NewContactFragment(), TAG);
}

Fragments should generally only communicate with their direct parent activity. Fragments communicate through their parent activity allowing the activity to manage the inputs and outputs of data from that fragment coordinating with other fragments or activities. Think of the Activity as the controller managing all interaction with each of the fragments contained within.
A few exceptions to this are dialog fragments presented from within another fragment or nested child fragments. Both of these cases are situations where a fragment has nested child fragments and that are therefore allowed to communicate upward to their parent (which is a fragment).
The important thing to keep in mind is that fragments should not directly communicate with each other and should generally only communicate with their parent activity. Fragments should be modular, standalone and reusable components. The fragments allow their parent activity to respond to intents and callbacks in most cases.
There are three ways a fragment and an activity can communicate:
Bundle - Activity can construct a fragment and set arguments
Methods - Activity can call methods on a fragment instance
Listener - Fragment can fire listener events on an activity via an interface
In other words, communication should generally follow these principles:
Activities can initialize fragments with data during construction
Activities can pass data to fragments using methods on the fragment instance
Fragments can communicate up to their parent activity using an interface and listeners
Fragments should pass data to other fragments only routed through their parent activity
Fragments can pass data to and from dialog fragments
Fragments can contain nested child fragments
Read more about Fragment and its communication at Creating and Using Fragments

Related

How to shift to another fragment from tab fragment created using view pager

I created three tabs with three fragments using view pager.I want to jump to inspiring fragment after clicking on list item defined inside Categories Fragment(Tab Fragment created with view pager).When i click on the list item error occurs.I want to jump to inspiring fragment from categories fragment(fragment defined inside view pager).
Categories (Tab Fragment created with view pager)
public class Categories extends Fragment {
private RecyclerView recyclerView;
private List<CategoriesDataModel> list;
private String[] categories={"Inspiring","Feelings","Strength","Hard Work","Success"};
public Categories() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_categories, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.categoriesList_Id);
list = new ArrayList<>();
for (int i = 0; i < categories.length; i++) {
CategoriesDataModel dataModel = new CategoriesDataModel();
dataModel.cat_name = categories[i];
list.add(dataModel);
}
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
CategoryRecyclerViewAdapter adapter = new CategoryRecyclerViewAdapter(list,getContext());
adapter.setOnItemClickListener(new CategoryRecyclerViewAdapter.ClickListener() {
#Override
public void onItemClick(int position, View v) {
switch (position){
case 0:
getFragmentManager().beginTransaction().replace(R.id.frameLayout_inspiring,new Inspiring()).addToBackStack(null).commit();
}
}
});
recyclerView.setAdapter(adapter);
return view;
}
}
Inspiring :-
public class Inspiring extends Fragment {
public Inspiring() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_inspiring, container, false);
}
}
Pager Adapter :-
public class Pager extends FragmentStatePagerAdapter {
int tabCount=0;
public Pager(FragmentManager fm,int tabCount) {
super(fm);
this.tabCount=tabCount;
}
//this will return tab selected
#Override
public Fragment getItem(int i) {
switch(i) {
case 0:
return new Recents();
case 1:
return new Top();
case 2:
return new Categories();
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
You might find the AndroidViewModel of use in this case.
What you are doing is attempting to maintain state between different parts of your app.
If you have an AndroidViewModel attached to the Activity Lifecycle, you can observe that state in your Activity and make transactions to the FragmentManager to represent your choices.
An example
ViewModel
This ViewModel contains state data for which navigation item you are in (representing a Fragment with an integer in this case) and using an integer to represent an index for your inspiration row.
public class NavigationViewModel extends AndroidViewModel {
private MutableLiveData<Integer> navigationLiveData = new MutableLiveData<>();
private MutableLiveData<Integer> inspirationLiveData = new MutableLiveData<>();
public NavigationViewModel(#NonNull Application application) {
super(application);
}
public LiveData<Integer> getNavigation() {
return navigationLiveData;
}
public void setNavigation(Integer id) {
navigationLiveData.postValue(id);
}
public LiveData<Integer> getInspiration() {
return inspirationLiveData;
}
public void setInspiration(Integer id) {
inspirationLiveData.postValue(id);
}
}
Activity
The Activity will observe the navigation LiveData provided by our implementation of the AndroidViewModel. This will let it know immediately when a navigation change has been made.
public class NavigationActivity extends AppCompatActivity {
private NavigationViewModel navigationViewModel;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigationViewModel = ViewModelProviders.of(this).get(NavigationViewModel.class);
navigationViewModel.getNavigation().observe(this, id -> {
switch(id) {
case R.id.recents:
// TODO: Load recent fragment here with a transaction
break;
case R.id.top:
// TODO: Load top fragment here with a transaction
break;
case R.id.categories:
// TODO: Load categories fragment here with a transaction
break;
case R.id.inspiring:
// TODO: Load inspiring fragment here with a transaction
break;
}
});
}
}
Inspiration Fragment
This Fragment will observe the inspiration index provided by our implementation of AndroidViewModel. That lets it know what content needs to be displayed. This can be set from ANYWHERE.
public class InspiringFragment extends Fragment {
private NavigationViewModel navigationViewModel;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_inspiring, container, false);
navigationViewModel = ViewModelProviders.of(this).get(NavigationViewModel.class);
navigationViewModel.getInspiration().observe(getViewLifecycleOwner(), inspiration -> {
// TODO: Update the root view UI with data gleaned using the inspiration index given here
});
return root;
}
}
Setting it
Once you have that, all you need to do is call:
navigationViewModel.setInspiration(1);
navigationViewModel.setNavigation(R.id.inspiration);
This should give you a good base to start with.

Used to Add a fragment A to fragment B and go back with the result to fragment A [duplicate]

I am developing an Android application. I have a requirement like there is a button in fragment 1, when a user clicks that button result should be displayed in fragment 2. While loading the activity both fragments is attached. Here is my try:
In main activity:
public void dsp(String str) {
secondfragment f2=new secondfragment();
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
f2.setArguments(bundle);
}
In first fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragone, container,false);
Button btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
m.dsp("clicked");
}
});
return v;
}
In second fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragtwo, container,false);
tv= (TextView) v.findViewById(R.id.textView1);
tv.setText(this.getArguments().getString("name"));
return v;
}
When communicating from Fragment to Fragment you use an interface to pass data to the Activity which in turn updates the fragment you want to change.
For Example:
In Fragment 1:
public class FragmentOne extends Fragment{
public Callback mCallback;
public interface Callback{
void onUpdateFragmentTwo(String message);
}
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (Callback) activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragone, container,false);
Button btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mCallback.onUpdateFragmentTwo("clicked");
}
});
return v;
}
}
then in main Activity implement the interface:
public class MainActivity extends AppCompatActivity implements Callback{
FragmentTwo fragmentTwo;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ... Load views or perform logic
// ... Load Fragment Two into your container
if(savedInstanceState == null){
fragmentTwo = FragmentTwo.newInstance(new Bundle()); // use real bundle here
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_holder, fragmentTwo, "Frag2").commit();
}
}
// Interface method
#Override
public void onUpdateFragmentTwo(String message){
// Call activity method with the argument
if(fragmentTwo != null){
fragmentTwo.updateFragmentTwo(message);
}
}
}
Update
In your second fragment I typically use a static newInstance(Bundle args) method to initialize and then would use a public method to communicate from the Activity to the Fragment for example:
public class FragmentTwo extends Fragment{
public static FragmentTwo newInstance(Bundle args){
FragmentTwo fragment = new FragmentTwo();
fragment.setArguments(args);
return fragment;
}
//... Class overrides here onCreateView etc..
// declare this method
public void updateFragmentTwo(String updateText){
// .. do something with update text
}
}
Thats it, happy coding!
Here you have what the Android Documentation says about Communicating Between Fragments. Here you'll have all the necessary steps to make two or more fragments communicate securely :)

EventBus in viewPager fragment issue

I have thee fragments in viewpager, Fragment1, Fragment2, and Fragment3.
fragment1 has a button and when onclick it setEnable(true) a button in fragment2 and the button in fragment 2 setEnable(true) a button3 in fragment3
All seem to work fine, but when I navigate through the fragments when passing from fragment 3 to fragment 1, fragment3 lose the information and the button comes back to its initial state -setEnable(false) as in the beginning, but fragment1 and fragment2 still work fine and don't lose the information when I navigate through all the fragments in viewpager.
How can I keep the information in fragment3 while navigating?
Here is my code:
fragment1
public class FragmentUno extends Fragment {
Button btnClick;
private EventBus bus = EventBus.getDefault();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_uno, container, false);
btnClick = (Button)rootView.findViewById(R.id.button1);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bus.post(new EventoBotton());
System.out.println("saliendoooo");
}
});
return rootView;
}
}
fragment2
public class FragmentDos extends Fragment {
private EventBus bus = EventBus.getDefault();
private EventBus bus2 = EventBus.getDefault();
Button btnNext;
#Override
public void onResume() {
super.onResume();
bus.register(this);
}
#Override
public void onPause() {
super.onPause();
bus.unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dos, container, false);
btnNext = (Button)rootView.findViewById(R.id.btnF2);
btnNext.setEnabled(false);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//HERE I POST EVENT TO SEND TO FRAG 3
bus2.post(new EventoDos());
}
});
return rootView;
}
#Subscribe
public void reenvio(EventoBotton ev) {
btnNext.setEnabled(true);//HERE RECEIVE EVENT FORM FRAG 1
}
}
Fragment3
public class FragmentTres extends Fragment {
TextView txtDos;
Button btnDis;
private EventBus bus2 = EventBus.getDefault();
#Override
public void onResume() {
super.onResume();
bus2.register(this);
}
#Override
public void onPause() {
super.onPause();
bus2.unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tres, container, false);
txtDos = (TextView)rootView.findViewById(R.id.txtCambio);
btnDis = (Button)rootView.findViewById(R.id.buttonF2);
btnDis.setEnabled(false);
return rootView;
}
#Subscribe
public void actualizar(EventoDos eventoDos) {
System.out.println("estamos aquiiii"); // Here I receive the event from frag 2
txtDos.setText("hola mundo");
btnDis.setEnabled(true);
}
}
These are my POJO events
public class EventoBotton {
public EventoBotton() {}
}
The other event
public class EventoDos {
public EventoDos() {}
}
I'm using eventbus 3.0 with my SDK 21.
All works fine but when I slip through the view pager, if I slide from 3 to 2, the information is still there in frag3. But when I slide from 3 to 2 and reach 1 when I come back to 3, the information is lost. That is to say, the button in frag 3 come back to its original state (enabled false) Why?? How can I persist the changes in all the navigation in the viewpager?
You can try using:
setOffscreenPageLimit(int limit) // In your case, limit = 2
on the ViewPager object which sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

Can't Access to ExpandablelistView in fragment

MainActivity on startup add a fragment layout to Relativeview, then i send a data to fragment to add it to ExpandablelistView but my app shows me error that couldn't recognize ExpandablelistView.
MainActivity:
public class MainActivity extends AppCompatActivity implements FragmentAddCatergory.onClickButtonListener {
private FragmentManager manager;
private FragmentTransaction transactionShowList;
private FragmentTransaction transactionAddCatergory;
private FragmentAddCatergory addCatergory;
private FragmentShowCategory showCategory;
private boolean addcategory;
private TextView txtAddCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
transactionShowList = manager.beginTransaction();
showCategory = new FragmentShowCategory();
addCatergory=new FragmentAddCatergory();
transactionShowList.add(R.id.Fragment_container, showCategory);
transactionShowList.commit();
addcategory=false;
txtAddCategory = (TextView) findViewById(R.id.txtaddcategory);
txtAddCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ChangeFragment();
}
});
}
public void ChangeFragment(){
transactionAddCatergory=manager.beginTransaction();
if (addcategory){
transactionAddCatergory.replace(R.id.Fragment_container,addCatergory);
txtAddCategory.setText("Do you want to see your List?Show me!");
addcategory=false;
}else{
transactionAddCatergory.replace(R.id.Fragment_container,showCategory);
txtAddCategory.setText("Do you want to add a Category?Create One");
addcategory=true;
}
transactionAddCatergory.commit();
}
#Override
public void ClickButton(String group, String child) {
FragmentShowCategory a=new FragmentShowCategory();
a.showExpand(this,group,child);
}}
in last above code i make object from first fragment and send a data and in below code is code of first fragment
public class FragmentShowCategory extends Fragment {
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.activity_expandable_list_view, container, false);
return view;
}
public void showExpand(Context context, String g, String c) {
Toast.makeText(context, g + " is " + c, Toast.LENGTH_LONG).show();
HashMap<String, List<String>> carsDetails = DataProvider.getInfo(g, c);
List<String> carsBrands = new ArrayList<String>(carsDetails.keySet());
ItemClass adapter = new ItemClass(context, carsDetails, carsBrands);
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.expandList);
list.setAdapter(adapter);
}}
but when i ran my app, i get error that i don't know why in line of:
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.expandList);
i'd appreciate to help me.
Your fragment's view hierarchy is not inflated automatically just because you created an instance of your fragment, as you do in ClickButton. The onCreateView() method that has to be called first in order to inflate your views is part of the fragment's lifecycle. You should let Android instantiate your fragment, and acquire it's instance through the FragmentManager.
This tutorial explains basics about fragments very well.

Accessing Fragment's views after inflating

What I need to achieve
A screen displaying a ListView, which can be replaced by an error screen in case of problems (missing connection, server unavailable and the like).
I need to be able to switch (programmatically) back and forth between these two screens.
Requirements
The main screen must be a Fragment.
This is because my application is composed of several sections, each one accessible from the navigation drawer.
What I have done so far
The main fragment class is named AllQueuesFragment: its XML layout consists of a FrameLayout, which I use in combination with the FragmentManager to switch between ErrorFragment (containing the error message) and QueuesViewFragment (containing the ListView).
public class AllQueuesFragment extends Fragment
{
public AllQueuesFragment()
{
super();
}
#Override
public void onStart()
{
super.onStart();
// Show the right fragment based on connectivity status
checkConnection();
}
public void checkConnection()
{
final NetworkManager netManager = NetworkManager.getInstance(this.getActivity());
if (netManager.isConnected())
showQueues();
else
showNoConnection();
}
public void showNoConnection()
{
ErrorFragment fragNoConnection = new ErrorFragment();
displayFragment(fragNoConnection);
fragNoConnection.setTitle(R.string.text_no_connection);
fragNoConnection.setIcon(R.drawable.thatfeel);
fragNoConnection.setLoaderVisibility(false);
}
public void showQueues()
{
QueuesViewFragment fragQueuesView = new QueuesViewFragment();
displayFragment(fragQueuesView);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_allqueues, container, false);
return rootView;
}
// Displays a new fragment
public void displayFragment(Fragment fragment)
{
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
}
}
The error screen is the following:
public class ErrorFragment extends Fragment
{
private TextView textTitle;
public ErrorFragment()
{
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_error, container, false);
// Get the widgets
textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
return rootView;
}
// Set methods
public void setTitle(int id) { textTitle.setText(id); }
}
The problem
The setTitle() method gets called before the layout is ready, and as a result, a NullPointerException is thrown.
class AllQueuesFragment
{
....
public void displayFragment(Fragment fragment)
{
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
}
public void showNoConnection()
{
ErrorFragment fragNoConnection = new ErrorFragment();
displayFragment(fragNoConnection);
// PROBLEM HERE: Before calling setTitle(), I must be sure that ErrorFragment's
// layout is inflated!
fragNoConnection.setTitle(R.string.text_no_connection);
}
....
}
class ErrorFragment
{
....
public void setTitle(String value) { textTitle.setText(value); }
....
}
I can't call setTitle() directly from ErrorFragment::onCreateView(), because I don't know beforehand which message I need to show.
How can I ensure that fragNoConnection has completed its layouting?
Is there a better way to achieve my goal?
Unsatisfying workaround
The only workaround I can think of is to use a buffer to defer the actual call:
class ErrorFragment
{
// This string will hold the title until the layout is inflated
private String titleBuffer;
private TextView textTitle = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_error, container, false);
// Get the widgets
textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
// Do the actual set
setTitle(titleBuffer);
return rootView;
}
....
public void setTitle(String value)
{
titleBuffer = value;
// If the layout is not inflated, defer the actual set
if (textTitle != null)
textTitle.setText(titleBuffer);
}
....
}
but I don't like this solution very much (the code above is simplified; ErrorFragment has more properties).
Advices?
Thanks in advance
This is exactly the type of thing arguments are supposed to be used for:
public void showNoConnection() {
ErrorFragment fragNoConnection = new ErrorFragment();
Bundle args = new Bundle();
//you can also use putInt here if you'd rather pass a string resource id, along with anything else you can stick into a Bundle
args.putString("title", "some title");
fragNoConnection.setArguments(args);
displayFragment(fragNoConnection);
}
Then in ErrorFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_error, container, false);
TextView textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
//now retrieve the argument...
textTitle.setText(getArguments().getString("title"));
return rootView;
}
The Fragment will even remember it's arguments after an orientation change.
If you feel like being pedantic, you can create a static factory method within ErrorFragment that takes the title as an argument and then creates the Fragment and adds the argument, that way you can achieve proper encapsulation :)
You need to have a callback method in your ErrorFragment and when the view is inflated you then call the method in your callback interface in the onViewCreated and set the title.
sample:
in ErroFragment
public class ErroFragment extends Fragment
{
static interface ErrorDone{
public void doneInflating();
}
private TextView textTitle;
private ErrorDone ed;
public ErroFragment()
{
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_error, container, false);
// Get the widgets
textTitle = (TextView)rootView.findViewById( R.id.fragment_error_text );
return rootView;
}
// Set methods
public void setTitle(int id) { textTitle.setText(id); }
public void setInterFace(ErrorDone er){ this.ed = er; }
}
Then you implement the interface in your AllQueuesFragment
public class AllQueuesFragment extends Fragment implements ErroFragment.ErrorDone
It will generate method doneInflating
and you need to set the interface:
public void showNoConnection()
{
ErrorFragment fragNoConnection = new ErrorFragment();
displayFragment(fragNoConnection);
fragNoConnection.setInterFace(this);
}
And in the generated method(doneInflating) of the AllQueuesFragment you then set the title in there:
public void doneInflating(){
fragNoConnection.setTitle(R.string.text_no_connection);
fragNoConnection.setIcon(R.drawable.thatfeel);
fragNoConnection.setLoaderVisibility(false);
}
If you want to be sure that the FragmentTransaction is commited and effective, you can use the executePendingTransactions method:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
fragmentManager.executePendingTransactions();
But, the right way to do it is to send the title value to the Fragment when instantiating it. This is the default pattern when you create a Fragment from your IDE (eclipse or Android Studio)

Categories

Resources