Views in fragment not showing up when fragment is popped from backstack - java

I have two fragments: one for entering names, and one that uses the list of names. On the second fragment there is a previous button to go back to the first fragment to edit the names. When the "previous" button is pressed, every programmatically generated EditText disappears, except for the one specified in the xml.
The first fragment before pressing next
The second page with the previous button
The first fragment after previous is pressed
This is how I'm inflating the Edittexts:
EditText editText = (EditText)getActivity().getLayoutInflater().inflate(R.layout.edittext,null);
editText.setHint("Roommate " + counter);
editText.requestFocus();
layout.addView(editText);
I've tried re-inflating completely new edittexts which still do not show up. Any suggestions would be fantastic
EDIT: Instead of replacing fragment 1 with fragment 2 I tried using getSupportFragmentManager().add(...) instead of .replace(...) and It now works as it should

Related

after returning back from my second Activity to first Activity on clicking button the whole data is gone

I have two activties:
activity_main,
activity_display
in the main_activity user enters his/her name and presses the toggle button to display.
that takes the user to display_activity page where the result is displayed.
if the user finds that he/she has entered the wrong info they can go back and edit.
here is where the problem starts
after pressing the toggle button - display
it goes to display_activity and displays but when I press "back" button I'm restarting the whole (first) main_activity again... no values are present...
even the toggle button that needs to be set to "reset" is again showing "display".
should i use something else instead of
startActivity(intend1);
i want to resume the (first) activity main not restart everything
Seems like your first activity uses results from second activity. you can use startActivityForResult. Here is a very good answer refer to it.
How to manage startActivityForResult on Android

How do I save the state of fragments in android?

I'm using a bottom navigation view which has 2 menu items. Each menu item upon clicking loads a fragment onto the MainActivity.
The first fragment is the home fragment where a RecyclerView shows a list of items and the second fragment contains a simple form.
now whenever I switch back and forth between these fragments, their previous states are lost and the fragment is recreated. This is a big issue for me because suppose I scroll to, let's say the 15th item of the list present in the home fragment. After scrolling, I navigate to the second fragment and then I navigate back. What happens is that the home fragment gets recreated and to see the 15th item I have to scroll again.
What should I do to handle this?

Android Java Fragment Button save

I got 2 Fragments. In the first Fragment I got a button.
How is it possible to duplicate the button on the second Fragment when the user click on it.
What do you mean by duplicating a Button? If it means creating a Button with the same text or other layout attribute, you can create a method in the second Fragment, like createButton(String text, int color), to add the button dynamically. When the button is clicked, find the second Fragment, cast it to the class of second Fragment and call the createButton method.

Putting ViewPager and tabs in a fragment and replacing it

In my main activity I have a ViewPager with two tabs and each of them has its contents in a fragment. One of the tabs holds a list of items and when I press on an item from that list I want the details of that item to appear on the screen. I thought of putting the whole ViewPager in a fragment and when the user taps the item the fragment representing the whole ViewPager is replaced with another fragment representing the details of the item. Is it possible and is it a good practice? Maybe I should just start another activity with the details of the item?
How you do it is your own choice. It's perfectly fine to switch your Fragments.
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FragDetails())
.addToBackStack(FragPager.class.getName()).commit();
Switching to another Activity is fine too, you just have more of a "flicking" screen when opening a new Activity (and some other life cycle stuff too of course). If you don't want that, switch Fragments.

Force Fragment refresh on tab change

I have two tabs. In the second one I have a SeekBar and a save button. When the save button is pressed, I save the SeekBar value and switch to the first tab using this in the save button click listener.
getActivity().getActionBar().setSelectedNavigationItem(0);
The Fragment at the position 0 has a ListView that gets values from SQLite database. That's the place where the SeekBar value was saved in the second Fragment. How do I make this Fragment refresh when the save button is pressed so that the changes are shown?
How do I make this Fragment refresh when the save button is pressed so that the changes are shown?
Just tell it to do that using interface of your design this fragment should implement.
mySecondFragment.refreshStuffPlease();
On tab press call the notifyDatasetChanged method of Base adapter of the ListView.
listadapter.notifyDatasetChanged()

Categories

Resources