I read a lot of discussion about that, and someone suggests to use Bundle to pass data between two fragments in the same activity...I tried some come, but it didn't worked.
I have 2 fragments in my GroupDetailActivity. The paymentFragment, which download data from a database and show them in a listView, and the fragment FragmentReport, which displays a report of the payments.
So, after the paymentFragment get the data, I need to pass some of them to the second fragment. That's my code:
PaymentFragment:
final Bundle bundle = new Bundle();
final FragmentReport fragment = new FragmentReport();
// This is inside the onCreateView, where I get the the data from the db and show them in a listview
bundle.putInt("num_of_pay", cont);
Log.v("cont", String.valueOf(cont)); // it display the correct number of person
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container, fragment);
Then, in my FragmentReport, inside the OnCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.report_fragment,container,false);
numberOfPaymentTV = rootView.findViewById(R.id.n_payment_tv);
Bundle args = this.getArguments();
if (args != null){
numberOfPaymentTV.setText(args.getInt("num_of_pay"));
Log.v("totalPayment", String.valueOf(args.getInt("num_of_pay"))); // I don't get any log, maybe this fragment doesn't receive the data
}
return rootView;
}
}
I don't get any error, but the textView doesn't show the "cont" value.
Maybe I could use sharedPreferences? Is it possibile achieve that with Bundle?
You can get args in onCreate() function.
private Bundle bundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
((TextView) view.findViewById(R.id.textView))
.setText(String.valueOf(bundle.getInt("KEY")));
return view;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.bundle = getArguments();
}
You should use the String.valueOf(int) when setting an int as text in a TextView:
numberOfPaymentTV.setText(String.valueOf(getArguments().getInt("num_of_pay"))
Related
Previously, I had a similar issue, app crashes when trying to access (or findByID), it turned out, I've put the textview in the wrong method, Now I have only one method, and I am working with a fragment, the method onCreateView and my app keeps crashing:
public class InfoFragment extends Fragment {
public InfoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView n = (TextView) getView().findViewById(R.id.nama);
n.setText("Some String");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info, container, false);
}
}
Originally, I had the line looking like this :
TextView n = (TextView) findViewById(R.id.nama);
However,the compiler gave me an error saying it can't find the method findViewById, so some research showed that I should add getView() , I did, and the error disappeared, but now the app is crashing, any ideas?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) view.findViewById(R.id.nama);
n.setText("Some String");
return view;
}
You don't have a view yet in that part of code. Inflate it first and use after.
Inflate your layout first, then you can use it to find the TextView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
TextView n = (TextView) rootView.findViewById(R.id.nama);
n.setText("Some String");
return rootView;
}
I've got a problem. For school project I have to create app in Android Studio with few activities so i decided to add Navigation Drawer, but when i try to create simple calculator to app i can't even reference button or textview. This is code for activity where I want to create calculator. Also I have to add Map Activity and SQLite Database.
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
return rootview;
}
}
Use rootView to find your item when referencing in a Fragment:
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
//Notice the TextView being referenced with rootView in front of the findViewById statement
TextView namefield = (TextView) rootView.findViewById(R.id.namefield);
return rootview;
}
}
You need to do this when using Fragment, as this is the only way your app can reference an item in your Fragment's XML view
This is however not necessary in an Activity and should not be confused with the method of referencing in a Fragment
I've got a problem with my Android app: my fragment content doesn't show up until I switch it and I select it again. What should load just at the fragment start is a list of Cards.
Here you got the onCreate method of the Fragment that loads everything:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = this.getArguments();
debts = bundle.getParcelableArrayList("list");
adapter = new CardAdapter(getActivity(), android.R.color.holo_blue_dark);
for (int i=0; i<debts.size();++i) {
Debt d = debts.get(i);
Card card = new Card(d.getSecondUser(),d.getSubject());
adapter.add(card);
}
}
And here the onActivityCreated method where i set the adapter for the list:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CardListView list = (CardListView) getView().findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
}
It works if I use an ArrayList of Cards already loaded, but not this way.
I tried nearly everything... thank you very much.
UPDATE: Even I tried with the solutions you gave me, it keeps happening, my code now:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = this.getArguments();
debts = bundle.getParcelableArrayList("list");
adapter = new CardAdapter(getActivity(), android.R.color.holo_blue_dark);
for (int i=0; i<debts.size();++i) {
Debt d = debts.get(i);
Card card = new Card(d.getSecondUser(),d.getSubject());
adapter.add(card);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_debts, container,
false);
CardListView list = (CardListView) rootView.findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
return rootView;
}
You should initialize your cardListView inside the onCreateView() method. onCreateView() gets called before the onActivityCreated() method. onCreateView() should be the point when the view of your fragment is initialized.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//use your layout file instead of R.layout.fragment_main
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
CardListView list = rootView.findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
}
Please take a look at the lifecycle of Fragments.
I have two fragments that contains ViewPager. The first fragment that contain viewPager works very well. As I come to second fragment that contain ViewPager, I start to get FragmentStatePagerAdapter NullException. I knew what is the problem, I need to implement onDetach function to get rid this problem. I search around the internet, there is a lots of solutions. And I'm currently using API 19. So maybe this solution very suitable for me. But I don't know how to use the solution.
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
What is exactly mFragment1 and mFragment2 and R.id.main ?
Fragment 1
public class fragment1 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_restaurantprocess, container, false);
adapter = new RestaurantProcessPageAdapter(getActivity().getSupportFragmentManager());
...
}
}
Fragment 2
public class fragment2 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_feedpage, container, false);
adapter = new RestaurantFeedPageFragment(getActivity().getSupportFragmentManager());
...
}
}
I'm using slide animation to fragment
public void changeFragment_toRight(Fragment frag) /* Slide to right screen */ {
if (getSupportFragmentManager().findFragmentByTag("frag")!=null)
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.right_slide_in, R.anim.right_slide_out).replace(R.id.container, frag, "frag").commit();
else
getSupportFragmentManager().beginTransaction().add(R.id.container, frag, "frag").commit();
}
changeFragment_toRight(new fragment1());
changeFragment_toRight(new fragment2());
Source: Click here to view the source
It looks like the problem is that you are manually using a FragmentTransaction to change fragments. A ViewPager will do this automatically when you supply it with an adapter, so when you change fragments, you are trying to remove a fragment that has already been removed.
I have a fragment, that i want to put a textview which will have a 3 strings stored in it from the 'Main Activity'
The strings that I need to pass will come from the Main Activity and will be passed into FragmentClass
The issue is that its not recognising findViewById, i understand this is something to do with the views? Could someone advise me on how i could get this editText working and then how i can pass a bundle of strings from the MainActivity to the FragmentClass.
#SuppressLint("NewApi")
public class FragmentClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.new_fragment, container, false);
// View rootView = inflater.inflate(R.layout.get_data, container, false);
TextView FragmentTextView = (TextView)findViewById(R.id.FragmentTextView);
}
}
Change to
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.new_fragment, container, false);
TextView FragmentTextView = (TextView)view.findViewById(R.id.FragmentTextView);
return view;
}
Also you have #SuppressLint("NewApi") you have suppressed lint warning. Probably indicates that some features are available in new api's only. Do check your min sdk version in manifest