Where Fragment save its State when FragmentTransaction.Replace? - android

Backgroud: i have a menu on the left, and different scrollable contents on the right. i wanted to save the scrolled position. But i failed. Then ...
I have set up a very simple project to test it.
In words, i have a menu on the left, and different contents all holding an <EditText> on the right. (of the same class though, they are of different instances)
In codes, (very simple)
content.xml
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please save my text ..." />
ContentFragment.java
package ... import ...
public class ContentFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.content, container, false);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
System.out.println("Why this is NOT called ?");
Log.v("onSaveInstanceState()", "outState = " + outState);
}
}
and the program generated MenuFragment.java
package ... import ...
public class MenuFragment extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[]{"Item 1", "Item 2", "Item 3"}));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
((MainActivity) getActivity()).click();
}
}
Lastly, our MainActivity.java
package ... import ...
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction()
.add(R.id.menu, new MenuFragment()).commit();
}
public void click() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, new ContentFragment() // everytime load a new instance
).addToBackStack(null).commit();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
System.out.println("... and this is NOT called neither :-/");
Log.v("onSaveInstanceState()", "outState = " + outState);
}
}
To test it, please DO NOT rotate your device.
First, choose an item from the menu, then type something in the textbox. Then choose another item and type something again. Repeat for a few times. Then navigate back and ...
you will find your texts previously typed ARE SAVED !!!
It is not surprised that the texts are saved ...
But it is surprised the texts are saved with all the onSaveInstanceState() functions NOT called.
When did Android save them while replaced by FragmentTransaction?
If i want to additionally save some data into each Fragment instance (like the scroll position), where should i put my codes?

By default, Edittext save their own instance. See Yalla T.'s answer.
How to retain EditText data on orientation change?

Related

Does FragmentStatePagerAdapter save fragment state on orientation change?

I have 3 fragments that need to be in a ViewPager. These fragment will hold dynamic information retrieved from a database. I understand that on an orientation change, the activity and fragments are destroyed and recreated. But I was under the impression by its name, that the FragmentStatePagerAdapter will save the state of the fragment. Apparently, I was wrong because every time I did something to the fragment, then change orientation, the fragment is reverted back to how it was laid out in the layout xml file.
As I was debugging, I noticed that on orientation change, the Adapter's getItem() method was never invoked - meaning that it wasn't recreated. So then how come the fragment state reverted back to its original state?
How do I save the fragment state using the FragmentStatePagerAdapter?
Please note that I have been following this tutorial and used their version of the SmartFragmentStatePagerAdapter.java class to manage the fragment dynamically.
And the following are my sample codes.
PageLoader.java - This interface allows MainActivity to manage the loading of the fragment pages dynamically at run time.
public interface PageLoader {
void loadPage(int from, int target);
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements PageLoader {
MyPagerAdapter adapter;
DirectionalViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
pager = (DirectionalViewPager) findViewById(R.id.vpPager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setOffscreenPageLimit(5);
pager.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int position = pager.getCurrentItem();
if (position > 0) pager.setCurrentItem(position - 1);
return true;
}
#Override
public void loadPage(int from, int target) {
PageLoader fragment = (PageLoader) adapter.getRegisteredFragment(target);
fragment.loadPage(from, target);
}
}
MyPagerAdapter.java
public class MyPagerAdapter extends SmartFragmentStatePagerAdapter {
private static final int NUM_ITEMS = 4;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show Frag1
return Frag1.newInstance(position, Frag1.class.getSimpleName());
case 1: // Fragment # 0 - This will show Frag1 different title
return Frag1.newInstance(position, Frag1.class.getSimpleName());
case 2: // Fragment # 1 - This will show Frag2
return Frag2.newInstance(position, Frag2.class.getSimpleName());
default:
return Frag3.newInstance(position, Frag3.class.getSimpleName());
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
Frag1.java Frag2.java Frag3.java - these are all the same, except for the numbering.
public class Frag1 extends Fragment implements PageLoader {
// Store instance variables
private String title;
private int page;
private TextView txtView;
// newInstance constructor for creating fragment with arguments
public static Frag1 newInstance(int page, String title) {
Frag1 fragmentFirst = new Frag1();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
// Inflate the view for the fragment based on layout XML
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
txtView = (TextView) view.findViewById(R.id.txt_frag1);
txtView.setText(page + " - " + title);
Button btn = (Button) view.findViewById(R.id.btn_frag1);
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
PageLoader activity = (PageLoader) getActivity();
activity.loadPage(page, page+1);
}
});
return view;
}
#Override
public void loadPage(int from, int target) {
txtView.setText(txtView.getText() + "\nThis message was created from" + from + " to " + target);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.someone.smartfragmentstatepageradapter.custom.DirectionalViewPager
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.someone.smartfragmentstatepageradapter.custom.DirectionalViewPager>
</LinearLayout>
frag1.xml frag2.xml frag3.xml - again these are all the same except for the numbering
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#cc2">
<TextView
android:id="#+id/txt_frag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="txt_frag1"
/>
<Button
android:id="#+id/btn_frag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="btn_frag1"
android:textSize="26dp" />
</LinearLayout>
PLEASE tell me how I can use the FragmentStatePagerAdapter to save the "State" of my fragments. I've been scouring the internet from 9am to 9pm today... 12 hours... I really need some help figuring this out. Thanks in advance!
EDIT Try this:
Add another instance variable to your fragment:
private String text; // this is part of saved state
Set this variable in loadPage:
#Override
public void loadPage(int from, int target) {
text = txtView.getText().toString() + "\nThis message was created from" + from + " to " + target;
txtView.setText(text);
}
Override onSaveInstanceState to save this variable:
#Override
public void onSaveInstanceState(Bundle outState);
outState.putString("text", text);
super.onSaveInstanceState(outState);
}
Then restore the the TextView state using this variable:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
// not null means we are restoring the fragment
text = savedInstanceState.getString("text");
} else {
text = "" + page + " - " + title;
}
View view = inflater.inflate(R.layout.frag1, container, false);
txtView = (TextView) view.findViewById(R.id.txt_frag1);
txtView.setText(text);
Button btn = (Button) view.findViewById(R.id.btn_frag1);
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
PageLoader activity = (PageLoader) getActivity();
activity.loadPage(page, page+1);
}
});
return view;
}
Any time you want something in your fragment to stay the same when things like configuration changes occur, this is how you would track the state, then save and restore it.
This is where you would use onSaveInstanceState for fragments and activities.
This is the method you would override to save any necessary state. Anything you change in your fragment that you want to have recreated on configuration change must be saved and then restored during onCreate or onCreateView.
So if you're trying to restore the text created in loadPage, you would create a class-level String for the text, set it in loadPage, save that in the onSaveInstanceState override, and then restore in in onCreateView from the savedInstanceState parameter.
Now here's the kicker: You are noticing that getItem on your adapter isn't called after a config change. But did you notice that your fragment is still there (even though it wasn't how you left it)? Keep in mind that the activity has a FragmentManager that is managing the fragments and their transactions. When the activity goes to config change, it saves its state. The FragmentManager and all of the active fragments are part of that state. Then the fragments are restored in such a way that adapter.getItem isn't called.
Turns out, that SmartFragmentPagerAdapter isn't so smart. It can't recreate its registeredFragments array after a configuration change, so it's really not very useful. I would discourage you from using it.
So how do you send events to off-page fragments when the ViewPager has appropriated the fragment's tag for its own use?
The technique I use is to define event listener interfaces, and have the fragments register as listeners with the activity. When I fire an event, it's by calling a method on the activity that notifies its active listeners. I give a pretty complete example of this in this answer.

Is it possible to convert Blank Activities to Fragment Activities

I currently have an Unit Converter app that I'm working in.
Here I've used multiple Blank Activities. Where each Unit's Activity can be opened using MainActivity. But now I want to make it tablet friendly.
Hence I want to use FragmentActivity now. Is it possible to convert the Blank Activities to Fragment Activities.?
All you need to do is take all View-specific logic from the Activity to a Fragment, then load the Fragment in your Activity.
For example,
public class MainActivity extends Activity {
#InjectView(R.id.button)
public Button button;
#OnClick(R.id.button)
public void onButtonClick(View view) {
Toast.makeText(this, "Hello!", Toast.LENGTH_SHORT).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
}
This type of logic goes in
public class MainFragment extends Fragment {
#InjectView(R.id.button)
public Button button;
#OnClick(R.id.button)
public void onButtonClick(View view) {
Toast.makeText(this, "Hello!", Toast.LENGTH_SHORT).show();
}
#Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.inject(this, view);
return view;
}
}
And your Activity needs to display this fragment either statically, or dynamically. If you go dynamical, you'll need the following lines in your Activity:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
if(savedInstanceState == null) {
fm.beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
fm.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if(getSupportFragmentManager().getBackStackEntryCount() == 0) finish();
}
});
}
}
If you go static, then you need to specify the fragments in your layout XML for the activity.
http://developer.android.com/guide/components/fragments.html#Adding
I would visit the Android website as they give a fairly good explanation on how fragments work.
You can learn how to add them to your existing application by another Android link here.

Android: content view not yet created when using listview in fragment?

I have fragment with two tabs, Each tab has it's respective fragment. each tab has its own list which is load from server. but my apps encountered exception like
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oj.bs/com.oj.bs.ProjectFragActivity}: java.lang.IllegalStateException: Content view not yet created
I can't understand where is the problem. Can anyone tell me what am I doing wrong?
Following is my fragment class
public class ResidentialFragActivity extends SherlockListFragment implements ActionBar.TabListener{
ListView listview;
ListView resListviewId;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.residential_list);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saved) {
View view = inflater.inflate(R.layout.residential_list,container, false);
resListviewId = (ListView)view.findViewById(R.id.resProjListView);
projectList = new ArrayList<HashMap<String,String>>();
new LoadProjects().execute();
ListView listview = getListView();
listview.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Intent href = new Intent(getSherlockActivity(), ProjectDetailActivity.class);
String proj_id = ((TextView) view.findViewById(R.id.projectId)).getText().toString();
href.putExtra("proj_id", proj_id);
getSherlockActivity().startActivity(href);
}
});
if (container == null) {
return null;
}
return view;
}
public void onStart() {
super.onStart();
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(android.R.id.content,this,"residential");
ft.attach(this);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.detach(this);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
//inner class for network operation
private class LoadProjects extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
..........
//get data from the latest server
String jsonpProjList = ResdProj.makeHttpReqToSrvr(projectUrl, "POST", projRes);
return null;
}
#Override
protected void onPostExecute(String result) {
getSherlockActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
ListAdapter projAdapter = new SimpleAdapter(getSherlockActivity(),
projectList, R.layout.residential_list_item,
new String[] {PROJ_ID,PROJ_NAME}, new int[]
{R.id.projectId,R.id.projectName});
//updating the UI
setListAdapter(projAdapter);
}
});
}
}
Thanks in Adavance
First of all I can see lots of 'bad things' in your code. The first thing which I would never do and I'm not really sure that it will work is getActivity().setContentView(R.layout.residential_list); . You can add Fragments to FragmentActivity using xml or adding directly to a FrameLayout. Just check the example in Android for Fragments for best practice how to achieve this Fragments .
Second thing, no need to use this runOnUiThread in onPostExecute(), because it is running on UI thread already.
And last thing which I can think of first set content to your FragmentActivity, add your tabs to ActionBar and attach your Fragments on first / second tab. And after that you can properly populate your ListView's with data using AsyncTask.
I think this is the problem ,but not sure.
new LoadProjects().execute();
you are calling this before listview creating that's why there is no view so call after listview creation.
update:-
or call new LoadProjects().execute(); inside the
`onResume()`.it will get called after `onCreate()` or `onCreateView()`
So you have to remember you live in a crazy world where you can create the view for your Fragment, and it's detached from the Activity that contains it, so your although your fragment's view is ready it's not on screen and your "Content view not yet created".
You are going to want to implement onActivityCreated on your Fragment and call
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new LoadProjects().execute();
}
from there.
That will ensure that both your fragment and activity are created. BUT WAIT there's more. Also remember that you can leave your activity with a activity.startActivity(...), and your fragment may still be around (maybe detached maybe gone). In that case the onActivityCreated will get called again when your activity resumes and your fragment is reattached. So that might get called more often than you want now.
I'll leave it up to you for business logic on when to really trigger that, or maybe if you leave the activity it's okay to reload that content. Best of luck.

Fragment setRetainInstance not works (Android support lib) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android fragments setRetainInstance(true) not works (Android support library)
I wrote a simple test project, but I cant understand why I always receive savedInstanceState = null in lifecycle methods onCreate, onCreateView and onActivityCreated. I change the screen orientation, see the log, but state not saved.
Tell me please where is my mistake.
Thanks.
The code of fragment class is:
public class TestFragment extends Fragment {
private String state = "1";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
//never works
state = savedInstanceState.getString("state");
}
//always prints 1
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
return inflater.inflate(R.layout.fragment_layout, container, false);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("state", "2");
Log.e("", "saved 2");
}
}
EDIT
When i try to use setRetainInstance i have no result again((( I change a state to 2 using btn1, but after changing orientation i see 1 when pressing on btn2. Help please(
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
private String state = "1";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
//button for changing state
((Button)view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
state = "2";
}
});
//button for showing state
((Button)view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
I have seen the same issue. But you can save the instate state in an other way, thanks to the method setRetainInstance(true), all your class data are saved automatically.
#Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
EDIT
Try to also add these params when you declare your activities :
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize" />

ViewPager + FragmentStatePagerAdapter: ViewPager stops working when screen orientation changes

i am using ActionBarSherlock (which is basically an extension of the Android Support Package).
What i'm trying to do is the following:
I have a FragmentActivity which hosts just a single ViewPager. This ViewPager has a FragmentStatePagerAdapter (because there will be many items in the future). But for now it is just loaded with 2 items for testing.
Everything is working just fine while i am in portrait orientation. But when i change so landscape orientation it switches back to the first item in the adapter (which is fine since everything is reloaded etc), but i am unable to swype to the next item. There is just nothing happening.
From debugging i can see that the Loader return the two items just fine. getItem(...) is also called with position 0 and 1. So basicall everything looks fine, except it isn't ;)
Btw: the same thing is happening vice versa when i start in landscape orienation and switch to portrait orientation.
Any ideas what might be wrong here?
Here is some of my code:
public class QuotesStatePagerAdapter extends FragmentStatePagerAdapter {
private List<Quote> mQuotes;
public QuotesStatePagerAdapter(FragmentManager fm, List<Quote> quotes) {
super(fm);
mQuotes = quotes;
}
#Override
public Fragment getItem(int position) {
Bundle arguments = new Bundle();
arguments.putSerializable("quote", mQuotes.get(position));
QuoteFragment fragment = new QuoteFragment();
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
return mQuotes.size();
}
}
public QuotesFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void updateOrdering(ORDERING newOrdering) {
mOrdering = newOrdering;
getLoaderManager().getLoader(0).startLoading();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.quotes, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setOnPageChangeListener(this);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
#Override
public Loader<List<Quote>> onCreateLoader(int id, Bundle args) {
return new QuotesLoader(getActivity(), mCategoryId);
}
#Override
public void onLoadFinished(Loader<List<Quote>> loader, List<Quote> data) {
mQuotes = data;
mViewPager.setAdapter(new QuotesStatePagerAdapter(
getSupportFragmentManager(), mQuotes));
}
android:configChanges="orientation" worked like a charm, but i saw that it's not recommended by android team (only last case resource)... See here http://developer.android.com/guide/topics/manifest/activity-element.html#config
I found out that putting:
setRetainInstance(true); on the onCreateView of each fragment retained the instance do the trick. (please not that the onActivityCreated will be called again)
Try adding android:configChanges="orientation"to manifest inside Activity tag

Categories

Resources