viewPager.setCurrentItem() seems not working after saveOnIstanceState() - android

I have a fragment that implements a ViewPager, in this fragment, I save the ViewPager status using the onSaveInstanceState() method. I can save it in the bundle using viewPager.getCurrentItem(), but when I go to restore the status of the ViewPager, the viewPager.setCurrentItem() doesn't seem to work.
How can I save the fragment status correctly?
This is my fragment code
public class HomeFragment extends Fragment {
private ViewPager viewPager;
private ViewPagerAdapter adapter;
Integer[] colors=null;
List<Model> models;
private String[] mDescriptions;
private String[] mTitle;
private String mPlayerName;
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentItem", viewPager.getCurrentItem());
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
models = new ArrayList<>();
Resources res = getResources();
mDescriptions = res.getStringArray(R.array.newGameDescription);
mTitle = res.getStringArray(R.array.newGame);
models.add(new Model(R.drawable.ic_launcher_background,mTitle[0], mDescriptions[0]));
models.add(new Model(R.drawable.ic_launcher_background,mTitle[1], mDescriptions[1]));
models.add(new Model(R.drawable.ic_launcher_background,mTitle[2], mDescriptions[2]));
models.add(new Model(R.drawable.ic_launcher_background,mTitle[3], mDescriptions[3]));
adapter = new ViewPagerAdapter(getContext(), models, mPlayerName);
int orientation = getResources().getConfiguration().orientation;
adapter.setOrientation(orientation);
View view = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) view.findViewById(R.id.gameModeViewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
return view;
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state for checked position.
viewPager.postDelayed(new Runnable() {
#Override
public void run() {
int item = savedInstanceState.getInt("currentItem");
viewPager.setCurrentItem(item);
}
}, 100);
}
}
}
Any suggestion and review are welcome, thanks in advance

onCreateView does not return bundle value from onSaveInstanceState you have to get it from onActivityCreated
#public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state for checked position.
viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
int item = savedInstanceState.getInt("currentItem");
viewPager.setCurrentItem(item);
}
});
}

Related

Nested fragments with viewpager and bottom navigation

i am stuck at nested fragments , some one please guide me a little bit. i am in learning phase...
actually i am using a bottomNavigationView and popluate fragments and when selecting an item, one fragment has a view pager and tab layout in it, and loading list data from fire base. the problem is that when a fragment with viewpager in it selected, data fetched successfuly but not populated in list view, but when i swite view pages the listview is populated . any one please help me for this problem or guide me where to put network calls in fragment or something else solution of it.
First Fragment which have Viewpager in .
public class explore extends Fragment {
public static explore Instance = null;
private TabLayout tabLayout;
private ViewPager viewPager;
private Activity activity;
private Context context;
private PagerAdapter pagerAdapter;
public static explore getInstance() {
if (Instance == null) {
Instance = new explore();
}
return Instance;
}
public explore() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
activity = (Activity) getActivity();
context = getActivity();
return inflater.inflate(R.layout.fragment_explore, container, false);
}
#Override
public void onViewCreated(#NonNull View v, #Nullable Bundle savedInstanceState) {
tabLayout = v.findViewById(R.id.tabs_Explore);
viewPager = v.findViewById(R.id.viewpager_Explore);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
List<Category> categories = new FirebaseHelper(getActivity()).getCategoriesList();
pagerAdapter = new PagerAdapter(getChildFragmentManager(), categories);
viewPager.setAdapter(pagerAdapter);
super.onActivityCreated(savedInstanceState);
}
}
pager's first fragment means(ChildFragment)
public class categories extends Fragment {
private static categories Obj = null;
private RecyclerView listView;
private List<Category> categoryList;
private CategoryAdapter categoryAdapter;
public categories() {
categoryList = new ArrayList<>();
}
public static categories getInstance() {
if (Obj == null) {
Obj = new categories();
}
return Obj;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_blank, container, false);
listView = v.findViewById(R.id.mRecycler);
listView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
listView.setHasFixedSize(true);
SnapHelper helper = new LinearSnapHelper();
helper.attachToRecyclerView(listView);
categoryAdapter = new CategoryAdapter(getActivity(), categoryList);
listView.setAdapter(categoryAdapter);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
categoryList = bundle.getParcelableArrayList("data");
categoryAdapter.notifyDataSetChanged();
}
super.onActivityCreated(savedInstanceState);
}
}

loading fragment android best practice

I have a fragment which load data from API. I want to show loading progress as google play app does and hide or remove it when it is done.
first off all I don't want to use other libraries(http://johnkil.github.io/Android-ProgressFragment/) for this. I have bunch of code for that. I created a base fragment which show loading. but i am instantiating it in its own class. is it good to work in this way. however the loading implementation works great. MY Loading Fragment is :
public class SpinnerFragment extends Fragment {
private SpinnerFragment spinnerFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_progress, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinnerFragment = new SpinnerFragment();
}
/**
* Detach from view.
*/
#Override
public void onDestroyView() {
super.onDestroyView();
}
protected void addSpinner() {
if (!spinnerFragment.isAdded()) {
getFragmentManager().beginTransaction().add(R.id.content_frame, spinnerFragment)
.commit();
}
}
protected void removeSpinner() {
if (spinnerFragment.isAdded()) {
getFragmentManager().beginTransaction().remove(spinnerFragment).commit();
}
}
}
and i am using this class as follow:
public class List extends SpinnerFragment implements View.OnClickListener {
private ListAdapter listAdapter;
private ArrayList<ListRowItem> arrayList;
private ListView listView;
public List() {
}
public static List newInstance() {
List fragment = new List();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(getActivity()));
setRetainInstance(true);
addSpinner();
//loadDataFromWeb
String URL = new SqLite(getActivity()).returnDefaultURI() + "notice/"
new AsyncRequest(onAsyncRequestComplete, getActivity(), "GET", null,
null, 1).execute(URL);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return init(inflater.inflate(R.layout.notice_activity, container, false));
}
private View init(View view) {
listView = (ListView) view.findViewById(R.id.content_container);
listView.setOnScrollListener(onScrollListener());
footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.listview_loading_footer, listView, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (listAdapter == null && arrayList == null) {
arrayList = new ArrayList<>();
listAdapter = new ListAdapter(getActivity(), arrayList);
listView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
}
AsyncRequest.OnAsyncRequestComplete onAsyncRequestComplete = new AsyncRequest
.OnAsyncRequestComplete() {
#Override
public void asyncResponse(final String response, int apiKey) {
switch (apiKey) {
case 1:
//loading complete
listViewBinding(reasponse);
removeSpinner();
break;
}
}
};
}

Inflated layout from viewpager is null after created view

When I add the viewpager fragment everything goes nice, but when I move to another apps and returns to my app, I have a weird problem that getView or inflated is null. It is showing the view, but I can't have access to it.
Here comes the Fragment that will be used in the ViewPager:
public class FragmentRestauranteBandejao extends Fragment {
JSONObject dados;
View inflated;
ExpandableListView cardapio;
List<String> refeicoes;
List<String> pratos_almoco;
List<String> pratos_janta;
Map<String, List<String>> bandejao = null;
ExpandableListAdapter adapter;
public static FragmentRestauranteBandejao newInstance(String restaurante){
FragmentRestauranteBandejao frag = new FragmentRestauranteBandejao();
Bundle args = new Bundle();
args.putString("restaurante", restaurante);
frag.setArguments(args);
return frag;
}
public void getDados(){
if(dados == null){
if(USPRestaurantes.getDados()){
dados = USPRestaurantes.getCardapioByRestaurante(getArguments().getString("restaurante"));
}else{
Log.d("debug", "Dados ainda não estão prontos");
return;
}
}
if(inflated == null){
Log.d("debug", "inflated eh null");
if(inflated == null){
return;
}
}
//... some code here that deals with the data that comes from a request made at USPRestaurantes.
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflated = inflater.inflate(R.layout.fragment_cardapio, container, false);
return inflated;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
inflated = view;
prepararCardapio();
getDados();
}
public void prepararCardapio(){
cardapio = (ExpandableListView) getView().findViewById(R.id.cardapio);
cardapio.setClickable(true);
refeicoes = new ArrayList<String>(Arrays.asList("Almoço","Jantar"));
pratos_almoco = new ArrayList<String>();
pratos_janta = new ArrayList<String>();
pratos_almoco.add("");
pratos_janta.add("");
bandejao = new LinkedHashMap<String, List<String>>();
bandejao.put(refeicoes.get(0), pratos_almoco);
bandejao.put(refeicoes.get(1), pratos_janta);
adapter = new ExpandableListViewAdapter(getActivity(), refeicoes, bandejao);
cardapio.setAdapter(adapter);
}
ViewPager Fragment:
public class FragmentBandejoes extends Fragment{
private String[] tabs = {"Central", "Química", "Física"};
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ArrayList<Fragment> paginasBandeco;
public int dia_semana = 0;
ProgressDialog dialog;
private TabPageIndicator mIndicator;
int number = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paginasBandeco = new ArrayList<Fragment>();
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("central"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("quimica"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("fisica"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflated = inflater.inflate(R.layout.fragment_bandejoes, container, false);
return inflated;
}
public void notifyFragments(){
for(int i=0; i<3; i++){
((FragmentRestauranteBandejao) mAdapter.getItem(i)).getDados();
}
if(dialog != null){
dialog.dismiss();
}
}
public void getCardapios(boolean update){
if(!USPRestaurantes.getDados() || update){
boolean request_made = USPRestaurantes.getCardapio(getActivity());
}
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
getCardapios(false);
viewPager = (ViewPager) getView().findViewById(R.id.pager_bandejoes);
mAdapter = new TabsPagerAdapter(((MainActivity) getActivity()).fragmentManager, paginasBandeco, tabs);
viewPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator) getActivity().findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mIndicator.setCurrentItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
And Finally the FragmentStatePagerAdapter:
public class FragmentBandejoes extends Fragment{
private String[] tabs = {"Central", "Química", "Física"};
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ArrayList<Fragment> paginasBandeco;
public int dia_semana = 0;
ProgressDialog dialog;
private TabPageIndicator mIndicator;
int number = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
paginasBandeco = new ArrayList<Fragment>();
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("central"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("quimica"));
paginasBandeco.add(FragmentRestauranteBandejao.newInstance("fisica"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflated = inflater.inflate(R.layout.fragment_bandejoes, container, false);
return inflated;
}
public void notifyFragments(){
for(int i=0; i<3; i++){
((FragmentRestauranteBandejao) mAdapter.getItem(i)).getDados();
}
if(dialog != null){
dialog.dismiss();
}
}
public void getCardapios(boolean update){
if(!USPRestaurantes.getDados() || update){
boolean request_made = USPRestaurantes.getCardapio(getActivity());
}
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
getCardapios(false);
viewPager = (ViewPager) getView().findViewById(R.id.pager_bandejoes);
mAdapter = new TabsPagerAdapter(((MainActivity) getActivity()).fragmentManager, paginasBandeco, tabs);
viewPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator) getActivity().findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mIndicator.setCurrentItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
Additional Info: When I swipe to the other tabs, the view gets recreated and then it shows the data.
Thanks in advance
From what I can tell reading you code is that you are not taking into consideration the fragment's lifecycle.
Fragments, as Activities, have Lifecycle methods that you should use in order for them to render correctly. I suggest using the onResume callback and with it check if you have any data to display. If you do display them in your fragment. If you don't do what you do when you create the fragmet

how refresh view when change page?

i have a fragment activity that have 4 page and load from 4 link from internet
when program start customlist show lists but when i change page the view show white page
my fragment :
public class VpiAbsTestActivitynouser extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] {" page1","page2","page3","page4"};
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
TextView mSearchView;
public String[] xmlURLArray = new String[]{"link1.xml","link2.xml","link3.xml","link4.xml"};
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
Intent i = new Intent(this, transparentone.class);
startActivity(i);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.setCurrentItem(1);
}
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putString("url", xmlURLArray[position]);
return customlist.newInstance(args);
}
#Override
public int getCount() {
return mCount;
}
public CharSequence getPageTitle(int position) {
return VpiAbsTestActivitynouser.CONTENT[position % VpiAbsTestActivitynouser.CONTENT.length].toUpperCase();
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
}
}
and my custom list code is :
public class customlist extends SherlockFragment {
int fragment_position_in_viewpager = 0;
// All static variables
public static customlist newInstance(Bundle args) {
customlist fragment = new customlist();
fragment.setArguments(args);
return fragment;
}
ListView list;
LazyAdapterbeth adapter1;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
URL = getArguments().getString("url");
}
new getFeed().execute();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.dovomi, container, false);
return root;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getArguments() != null) {
fragment_position_in_viewpager = getArguments().getInt("position");
//Update the Fragment on ViewPager Position.
}
}
private class getFeed extends AsyncTask<Void, Void, Document> {
#Override
protected void onPreExecute() {
}
protected Document doInBackground(Void... params) {
}
protected void onPostExecute(Document doc) {
}
list=(ListView)getView().findViewById(R.id.list);
adapter1=new LazyAdapterbeth(getActivity(), songsList);
adapter1.notifyDataSetChanged();
list.setAdapter(adapter1);
how i can refresh view when change the view from page 1 to page 2 ?
As your code is very long, I didn't analyse it deeply but the following solutions come to my mind:
1) onCreateView for each fragment method is regularly called when fragment is recreated/its view hierarchy is recreated
Thus it can be one place for refreshing. However onCreateView is called slightly earlier than before paging displaying (usually where you display page nr 2, onCreateView for page number 3 is executed). The best idea is to put a short code displaying Toast in onCreateView and then you will know if it's ok for you.
2) Your main activity (extending FragmentActivity) can get 3 methods which are executed when swiping between fragments. It will look more or less in the following way:
public MyActivity extends FragmentActivity implements OnPageChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivityPageAdapter = new MainActivityPageAdapter (this, getSupportFragmentManager());
mainActivityViewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
mainActivityViewPager.setAdapter(mainActivityPageAdapter);
//set listener (for navigator)
mainActivityViewPager.setOnPageChangeListener(this);
}
//and now you have 3 methods
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
You can read more http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

How to edit a fragment and save its state?

I have to add TextViews and EditTexts in a Fragment ( Details() ) when i click in a button ( Button add ).
I was able to insert them, but when i swipe my 6 fragments and return in the first fragment this items disappear.
Can someone help me?
FragmentActivity class:
public class SubActivityTab extends FragmentActivity
{
SectionsPagerAdapter spa;
ViewPager vp;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolltab_layout);
spa = new SectionsPagerAdapter(getSupportFragmentManager(), this);
vp = (ViewPager) findViewById(R.id.pager);
vp.setAdapter(spa);
}
}
FragmentPagerAdapter class:
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
Context c;
public SectionsPagerAdapter(FragmentManager fm, Context c) {
super(fm);
this.c = c;
}
#Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return Details.newInstance();
...
}
return null;
}
#Override
public int getCount() {
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position)
{
case 0:
return c.getString(R.string.tab1).toUpperCase(l);
...
}
return null;
}
}
Fragment class:
public final class Details extends Fragment {
public static View layout;
public static LinearLayout ll;
public static Details newInstance()
{
return new Details();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
layout = inflater.inflate(R.layout.details, container, false);
ll = (LinearLayout) layout.findViewById(R.id.linearlayout2);
Button add = (Button) layout.findViewById(R.id.button_add);
add.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
insertLine();
insertClassEditText();
insertLevel();
}});
return layout;
}
private void insertLine()
{
View temp = (View) layout.findViewById(R.id.line);
View v = new View(temp.getContext());
v.setBackgroundColor(Color.rgb(209, 209, 209));
v.setId(0);
v.setLayoutParams(temp.getLayoutParams());
ll.addView(v);
}
private void insertClassEditText()
{
TextView label_classe = (TextView) layout.findViewById(R.id.textView10);
EditText edit_classe = (EditText) layout.findViewById(R.id.editText9);
TextView new_label_classe = new TextView(label_classe.getContext());
EditText new_edit_classe = new EditText(edit_classe.getContext());
new_label_classe.setId(1);
new_edit_classe.setId(2);
new_label_classe.setText("Label");
new_label_classe.setTextAppearance(new_label_classe.getContext(), android.R.style.TextAppearance_Medium);
ll.addView(new_label_classe);
new_edit_classe.setInputType(edit_classe.getInputType());
new_edit_classe.setLayoutParams(edit_classe.getLayoutParams());
ll.addView(new_edit_classe);
}
private void insertLevel()
{
TextView label_lv = (TextView) layout.findViewById(R.id.textView11);
EditText edit_lv = (EditText) layout.findViewById(R.id.editText10);
TextView new_label_lv = new TextView(label_lv.getContext());
EditText new_edit_lv = new EditText(edit_lv.getContext());
new_label_lv.setId(3);
new_edit_lv.setId(4);
new_label_lv.setText(label_lv.getText());
new_label_lv.setTextAppearance(new_label_lv.getContext(), android.R.style.TextAppearance_Medium);
ll.addView(new_label_lv);
new_edit_lv.setInputType(edit_lv.getInputType());
new_edit_lv.setLayoutParams(edit_lv.getLayoutParams());
ll.addView(new_edit_lv);
}
}
I believe ViewPager recycles the views as you swipe and therefore when you go back, it rebuilds from onCreateView.
I would suggest setting some tags and passing to onSaveInstanceState.
private void onSaveInstanceState(Bundle b) {
if (label_classe != null) {
b.putBoolean("label_classe", true);
}
}
Then in onCreateView you take savedInstanceState, check what elements were created and re-create them.
So in Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
....
if (savedInstanceState.getBoolean("label_classe")) {
insertClassEditText();
}
....
}
And just to be sure, in Activity:
public class SubActivityTab extends FragmentActivity
{
SectionsPagerAdapter spa;
ViewPager vp;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolltab_layout);
spa = new SectionsPagerAdapter(getSupportFragmentManager(), this);
vp = (ViewPager) findViewById(R.id.pager);
vp.setSaveState(true);
vp.setSaveFromParentEnabled(true);
vp.setAdapter(spa);
}
}
Try to change your onCreateView method to:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
if(layout ==null){
layout = inflater.inflate(R.layout.details, container, false); //Dont inflate every time.layout saved, data of layout,like EidtText also saved.
}
ViewParent parent = mLayout.getParent();
if(parent!=null){
((ViewGroup)parent).removeView(layout);//the view will be add to NoSaveStateFrameLayout after onCreateView,so remove self everytime
}
return layout;
}

Categories

Resources