Android fragment is not updating - android

I have a TextView into a fragment. When i update the text property from the activity, the component is not updated on fragment.
TextView amountTag = (TextView) fm.findFragmentByTag("currentFrag").getView().findViewById(R.id.amountTag);
amountTag.setText("another text");
fm variable is my FragmentManager.
I already tried to detach and attach the fragment again. But it does not work.

Check that you inflate your view in onCreateView of your fragment like this:
TextView amountTag;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_sample, container, false);
amountTag= (TextView) v.findViewById(R.id.amountTag);
return v;
}
If you have any issue create setAmountText in your fragment
public void setAmountText(String text) {
amountTag.setText(text);
}
An call it in your Activity
((CurrentFragment)fm.findFragmentByTag("currentFrag")).setAmountText("newValue");
Hope this helps!

I found the answer to problem here:
Handling onNewIntent in Fragment
The solution is send params to fragment, like:
Bundle bundle = new Bundle();
bundle.putString("amount", "newValue");
fragment.setArguments(bundle);
In the fragment:
Bundle bundle = this.getArguments();
if (bundle != null) {
amountTag.setText(bundle.getString("amount", "defaultValue"));
}
Thanks to everyone that helped!

Related

Fragment layout in android

I am using Fragment layout in my app. but I want to use The code that I written in Oncreate Bundle, so how I can write my code in OncreateView.
here is the code of fragment layout.
public class Text extends Fragment {
public Text(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);
return rootView;
}
so where I have to use oncreate bundle, or can I write same code in OncreateView.
help. Thanks.
You can get bundle like below if you passed some data by bundle.
Bundle bundle = getArguments();
We use this method for it.
public void onActivityCreated(Bundle savedInstanceState1) {
super.onActivityCreated(savedInstanceState1);
}
Here the full description about Fragment lifeCycle
Meanwhile, onCreateView must get the view from the Activity. here some usefull tutorial
wish that was helpfull
Fragment class doesn't have oncreate() method.You can write code in oncreateView().Check the Fragment lifecycle. Follow the link below
http://developer.android.com/guide/components/fragments.html

Go to previous fragment when press back button doesnt update FragmentAdapter

i have a activity A with fragment FA, from this fragment i go to FAB, this last fragment is a FragmentPagerAdapter.
When i go from A to FA to FB, press back button and return to FA, and go again to FB this fragment is not showing anything.
My getView method from pager adapter its not called.
This is my transactions code:
From A to FA:
private void setFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment replyGroupsFragment = new ReplyGroupsFragment();
Bundle bundle = new Bundle();
bundle.putString("title", reportName);
replyGroupsFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();
getSupportActionBar().setTitle(getString(R.string.info));
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
From FA to FB (Use butterkniffe)
#OnItemClick(R.id.listViewReplyGroup)
void listClick(int position) {
List<DomainReplie> domainReplieArrayList = generateRepliesList(position);
setRepliesFragment((ArrayList<DomainReplie>) domainReplieArrayList);
}
And my PagerFragment contains this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReplyGroupsActivity replyGroupsActivity = (ReplyGroupsActivity) getActivity();
List<DomainReplie> replies = getArguments().getParcelableArrayList("replies");
adapter = new PagerAdapter(replyGroupsActivity.getSupportFragmentManager(), this, replies);
setActionBar();
}
#Override
public void onResume() {
super.onResume();
FirextApplication.getInstance().getBus().register(this);
}
#Override
public void onPause() {
super.onPause();
FirextApplication.getInstance().getBus().unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_containter_replies, container, false);
ButterKnife.inject(this, view);
pager.setAdapter(adapter);
return view;
}
Life cyrcle on both case are equals, but in second round screen doesnt show anything.
A little bit late, but maybe it will help others.
I had the same problem. Finally I've solved it and I made the same mistake as you... Here is the line which is causing the problem:
fragmentManager.beginTransaction().replace(R.id.container, replyGroupsFragment, "ReplyGroupsFragment").commit();
You are calling replace(), but replace() just replaces current fragment - you have to call add() for adding another fragment to back stack - that's also what fixes ButterKnife to load/update views again.
Note: ButterKnife changes a lot! Use bind() instead of inject()

How to set the right fragment to replace.

I’m doing a navigation drawer in my android app. On my onNavigationDrawerItemSelected function i try on item select to change the current fragment into a new one. The problem is that when I change the current fragment its take the initial status of it. maybe because i'm doing this way
fragmentManager.beginTransaction()
.replace(R.id.cont, new SessionsFragement())
.commit();
I declare a new instance of my fragment and i think it’s normal, its display me the initial status of the thing. On my fragment i've try to set a textView to see if i will say it when i call the fragement from tha navigation drawer but it's not the case.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return inflater.inflate(R.layout.fragment_spot, container, false);
}
So please can anyone help me to fix this.
In the snippet you posted you are inflating again the view. You should return the one you modified
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return v;
}

How do I use the same fragment for three tabs with different content?

I have an enum describing three different sports:
public enum MatchType {
S1(0, "Sport1", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport1),
S2(0, "Sport2", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport2),
S3(0, "Sport3", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport3);
...getters/setters
}
I then have fragment with
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());
return inflater.inflate(matchType.getLayout(), container, false);
}
Also in my fragment I have an AsyncTask where I have this
#Override
protected void onPostExecute(final List<Match> matches) {
if (matches != null) {
matchListView = (ListView) getActivity().findViewById(matchType.getRId());
[setup listeners]
matchesArrayAdapter.matchArrayList = matches;
matchListView.setAdapter(matchesArrayAdapter);
}
}
EDIT:
In my Activity I have an AppSectionsPagerAdapter with
public Fragment getItem(int i) {
MatchListSectionFragment fragment = new MatchListSectionFragment();
Bundle bundle = new Bundle();
bundle.putInt(Constants.MATCH_TYPE, i);
fragment.setArguments(bundle);
return fragment;
}
EDIT 2:
Here's my onCreate and onCreateView from my fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());
return inflater.inflate(matchType.getLayout(), container, false);
}
The AsyncTask reads an xml stream for each of the sports in my enum but my problem is that tab #1 is overwritten with data from tab #2 and subsequently tab #3.
Before I had a fragment defined for each sport but surely that can't be necessary?
How do I go about using the same fragment with the same layout for each sport?
When instantiating your fragment in your Activity set the Fragment's arguments with a Bundle.
Bundle myBundle = new Bundle();
myBundle.putInt(MY_EXTRA, 1);
myFragment.setArguments(myBundle);
In your bundle put some Extra that will be read in the fragment's onCreate() callback.
int i = getArguments().getInt(MY_EXTRA);
I am on mobile.
Put three FrameLayouts in a LinearLayout, named frame1, frame2 and frame 3. This will be your main Activity's layout.
Then in the Activity's oncreate() method, call getFragmentManager().getFragmentTransaction().
Instantiate the three fragments and send them the data, preferably through a Bundle.
On the Fragment Transaction call the add() or replace() method for each fragment, the first parameter is the id of the respective FrameLayout, the second parameter is the fragment itself.
Call commit().
You should create the newInstance method in your fragment, also you should store MatchType instansce in you fragment.
MatchType matchType;
public static MyFragment newInstance(MatchType matchType) {
MyFragment fragment = new MyFragment();
fragment.matchType = matchType;
return fragment;
}
In your Activity you should to create 3 instances of MyFragment with this method (with related to each fragment it owns MatchType). Then in onCreateView method you should insert data to your views from matchType.
Sorry, I'm on mobile. And sorry for my English.
Update
Check your variable matchType. Maybe it declared as static?

Set new layout in fragment

I'm trying to change the layout of a fragment during runtime under a particular condition.
The initial layout in inflated within the onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cancel_video, null);
}
Then sometime later within the fragment code I would like to replace the initial layout with some other layout.
I've tried a few things so far; this is the latest that I have:
private void Something(){
if(checkLicenseStatus(licenseStatus, statusMessage)){
View vv = View.inflate(getActivity(), R.layout.play_video, null);
//more code
}
}
How can I accomplish this?
You cannot replace the fragment's layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like Fragments. Alternatively you can group all the layout elements into sub containers (like LinearLayout), then wrap them all in RelativeLayout, position them so they overlay each other and then toggle the visibility of these LinearLayouts with setVisibility() when and as needed.
Yes, I have done this in following way. When I need to set a new layout(xml), the following code snippet should be executed.
private View mainView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerObject, Bundle savedInstanceState){
super.onCreateView(inflater, containerObject, savedInstanceState);
mainView = inflater.inflate(R.layout.mylayout, null);
return mainView;
}
private void setViewLayout(int id){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainView = inflater.inflate(id, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(mainView);
}
Whenever I need to change the layout I just call the following method
setViewLayout(R.id.new_layout);
Use a FragmentTransaction through the FragmentManger
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to load in the list tab content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, new YourFragment());
ft.commit();
}
The code for the class YourFragment is just a LayoutInflater so that it returns a view
public class YourFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment, container, false);
return view;
}
}
I will change whole layout. You can change only a specific portion of your layout.
Add a root FrameLayout in your_layout.xml, it contains nothing.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Set your content in the java code.
ViewGroup flContent = findViewById(R.id.fl_content);
private void setLayout(int layoutId) {
flContent.removeAllViews();
View view = getLayoutInflater().inflate(layoutId, flContent, false);
flContent.addView(view);
}
You can change layoutId free at some point.
If you have some listeners, you must set again.

Categories

Resources