Keeping tab fragment view unchanged - android

I have a tabbed application using fragments. Following is a code snippet from one of the fragments in tabA
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
accountsView = inflater.inflate(R.layout.activity_accounts, viewGroup, false);
//obtain the labels- Top 50 Spending aacounts
spendingLabel = (TextView) accountsView.findViewById(R.id.textView1);
//get search box
accountSearch = (EditText) accountsView.findViewById(R.id.vAccountSearch);
return accountsView;
}
Here is the onStart()
#Override
public void onStart() {
super.onStart();
fillData();
}
fillData() fetches dat from the server. The application now fills a listView on tab click (by calling fillData() ). If I switch to other tabs and come to tabA, the page again calls fillData() and recreates the whole view.
How do I stop it reloading and keep the view unchanged on tab switching?
Since I am a newbie into Fragments, I find it hard to find a solution. Has anyone dealt this before?

According to the lifecycle of Android application try to override onCreate instead of onStart
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fillData();
}

Related

ListView is not updating in a fragment

I have 5 fragments in an activity. And they are being showed using tabs/viewpager. Suppose they are nameD as ONE, TWO, THREE, FOUR and FIVE. I am displaying data in all fragments as ListView. I am inflating data in ListView from database cursor. The data is updated normally when i perform an add or delete in the same fragment. The problem occurs when i send/move a data from one fragment to another.
EXAMPLE OF PROBLEM:
I send/move data from fragment ONE to fragment TWO. Then I tap on fragment TWO to view data. It is not there. The data is shown when I tap on fragment FOUR or fragment FIVE and then come back to fragment ONE. Or if the app is restarted or any other Activity comes in front and goes back.
The data is not shown by clicking the adjacent tabs or swapping to adjacent tabs. And then coming back to the tab from which data was moved
I am sure someone of you must have an idea whats happening here and tell me how to solve the issue.
onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_dramas, container, false);
setHasOptionsMenu(true);
list = (ListView) view.findViewById(R.id.mylist);
return view;
}
onResume
#Override
public void onResume() {
super.onResume();
getListView();
}
onViewCreated
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
add();
}
});
}
There are other methods in the fragment too but they are not relevant to the issue. I Think these three methods are relevant. And i have no onCreate() method in the fragment..
If Fragment is already in the memory you should use BroadcastReceiver to notify other fragments whenever any data is added/removed/updated.
You can try EventBus as well.
https://github.com/greenrobot/EventBus

Android Fragment from Layout - Retain variable data

I've been searching for hours and tried numerous methods but cannot seem to grasp my head around the idea / figure out how to retain/restore data in a ViewPager Fragment when it is destroyed and then recreated.
Here is what I have -
An activity where I setup the ViewPager and PageAdapter
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
//Setup pager and adapter
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
My PageAdapter where I setup a fragment with a bundle using .newInstance()
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
return fragment.newInstance(position);
}
My Fragment that has a layout that includes a TextView that shows the user a question, a picture, and two True/False buttons. New instance is returned back to the Adapter.
public static ScreenSlidePageFragment newInstance(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt("page_position", position + 1);
fragment.setArguments(args);
return fragment;
}
//the fragment is newly created for the first time or recreated when exiting the view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
//Handle a question being displayed on each fragment
count = getArguments().getInt("page_position") - 1;
mQuestionText = (TextView)v.findViewById(R.id.questionText);
mQuestionText.setText(bank.get(count).getQuestion());
//change the image depending on correct / incorrect answer
mPhoto = (ImageView)v.findViewById(R.id.imageView);
trueButton = (Button)v.findViewById(R.id.true_button);
falseButton = (Button)v.findViewById(R.id.false_button);
//True Button is pressed
trueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(bank.get(count).getAnswer()) {
mPhoto.setImageResource(R.drawable.right);
clickable = false;
}
else {
mPhoto.setImageResource(R.drawable.wrong);
clickable = false;
}
trueButton.setClickable(clickable);
falseButton.setClickable(clickable);
}
});
What I cannot figure out for the life of me, is how to retain/save that fact that the user has pressed a button and which picture to display when the fragment is restored. I have tried a number of options using onResume(), getArguments(), onSaveInstanceState(), onActivityCreated() etc but none of them seem to work.
I can fix the problem by keeping all my ViewPager pages alive using setOffscreenPageLimit(total pages) but have read this is a bad idea since it takes up a large amount of memory.
Here is an example that I use that can help you where an ArrayList of urls for pictures is saved for later when the view is recreated, you need to override onSavedInstanceState and save your variables in a bundle, then retrieve the values from the bundle when the view is created again, hope this can help
ArrayList<String> picutersUrl = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null)
{
picutersUrl = savedInstanceState.getStringArrayList("My Pictures");
}
#Override
protected void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putStringArrayList("My Pictures", picutersUrl );
}
I too have used PagerAdapter to support few static tab widgets. I think you need to add code to ScreenSlidePagerAdapter. Google webpage at PagerAdapter.
Note and read the details on override method instantiateItem. This is where you populate the UI.
Code example for ScreenSlidePagerAdapterfrom (PagerAdapter subclass), using your posted code:
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
...
trueButton = (Button)v.findViewById(R.id.true_button);
}
In reality with Views in this framework, you're responsible on saving the state of UI elements. In my ListView, I am caching and saving up to 100 rows of data and refreshes it in an Adapter, for example. For caching, I created custom class containing the UI data.

How to prevent the reload of a webView in a fragment when another fragment in the top was closed

I'm making an app with webviews.
I just have a single activity containing
a FrameLayout to dynamically add fragments.
Every fragment contains a webView.
Everything works fine, but when I remove a fragment
from the stack, the webview of the fragment in the
top of the stack is reloaded so the content inside the
script of the html is called again.
The restoreState() method is not working.
This is the onCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.wv_container, container, false);
webView = (WebView)view.findViewById(R.id.webviewcontainer);
if(webViewBundle == null)
webView.loadUrl("file:///android_asset/www/main.html");
else
webView.restoreState(webViewBundle);
return view;
}
This is the onPause() method:
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webViewContent.saveState(webViewBundle);
}
How should I prevent the page reloading for my case?
I don't want to hack the html with flags or something like that.
Thanks in advance!
The saveState method in your onPause returns a WebBackForwardList, which contains a list of WebHistory items. These are basically the titles, urls and favicons from recently visited pages. It does not cache the contents of the downloaded url.
Try looking at the method saveWebArchive instead.

How to clear ActionBar NavigationListMenu onPause() of fragment

Currenty, I have an activity with seven different child fragments in my android app. One of the fragment has to navigate a list menu but others do not have to navigate it or show it.
Frankly speaking, I don't want the other fragments use the NavigationListMode, however it will be activated also in all other fragments.
Is there anyway to perform this operation in Sherlock ActionBar Fragments?
I used the code snippet below to remove it for others, but I've never succeded (at anything what I have tried).
public class Fragment_Success extends SherlockListFragment implements ActionBar.OnNavigationListener
{
#Override
public void onPause()
{
super.onPause();
//academic_year_list.clear(); // Unsupported operation exception returned
getSherlockActivity().getSupportActionBar().getNavigationMode();
getSherlockActivity().closeOptionsMenu();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.layout_fragment_success, container, false);
...
String[] academic_year_array = getResources().getStringArray(R.array.academic_year);
Context context = getSherlockActivity().getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> academic_year_list = ArrayAdapter.createFromResource(context, R.array.academic_year, R.layout.sherlock_spinner_item);
academic_year_list.setDropDownViewResource(com.actionbarsherlock.R.layout.sherlock_spinner_dropdown_item);
getSherlockActivity().getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSherlockActivity().getSupportActionBar().setListNavigationCallbacks(academic_year_list, this);
...
return rootView;
}
...
}

Saving fragments on rotation when using an Async Task

The question pretty much sums it up, I am trying to save my fragments so when I rotate the screen the application does not crash but I am not sure where or how to save my fragments, I have tried using a fragment manager and setting retainstate to true as well as checking the saved instance state.
This is my code :
EventsActivty - Hosts the fragments
#Override
public void onCreate(Bundle savedInstanceState) {
new AsyncLogin().execute(username, password);
super.onCreate(savedInstanceState);
username = getIntent().getStringExtra("username");
password = getIntent().getStringExtra("password");
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
EventListFragment eventListFragment = (EventListFragment)
EventListFragment.instantiate(this, EventListFragment.class.getName());
EventGridFragment eventGridFragment = (EventGridFragment)
EventGridFragment.instantiate(this, EventGridFragment.class.getName());
fList.add(eventListFragment);
fList.add(eventGridFragment);
return fList;
}
The getFragments is called here, on the OnPostExecute of the AsyncTask
protected void onPostExecute(JSONObject jsonObject) {
try {
getEvents(jsonObject);
setContentView(R.layout.eventlist);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
Fragment 1 : OnCreateView
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
eventObjects = ((EventsActivity)getActivity()).getEventObjects();
setRetainInstance(true);
View view = inflater.inflate(R.layout.eventlist ,container,false);
final ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(new MyCustomBaseAdapter(getActivity(), eventObjects));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = listView.getItemAtPosition(position);
EventObject fullObject = (EventObject)o;
System.out.println("asd");
}
});
return view;
}
}
Fragment 2:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setRetainInstance(true);
View view = inflater.inflate(R.layout.eventgrid ,container,false);
GridView gridView = (GridView) view.findViewById(R.id.eventgrid);
gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
return view;
}
Actually, a FragmentActivity will automatically restore your fragments in onCreate().
Weird thing is, you call AsyncTask first, prior to calling through to super.onCreate() and retrieving username and password from the Intent.
Even having set that aside, that approach will make your activity spawn a new login task every time it's rotated.
Better way is to check savedInstanceState for null:
if (savedInstanceState == null) {
// first launch
new AsyncLogin().execute(username, password);
}
...
That way it's only going to run when Activity is created for the first time.
Second, you need to completely unbind login info from your activity. Make the AsyncTask return whatever login result you get to the Application and store it there. And your activity and fragments to retrieve that info from the Application - that way you get full control over you login procedure: you check whether there's an active session, if there isn't you check whether the AsyncTask is already running, if it isn't - you need to launch it. If it is - you need to wait for it to finish (show progress bar or something).
You should understand that retaining the fragments won't prevent the activity from being distroyed.
Now take a look here:
#Override
public void onCreate(Bundle savedInstanceState) {
new AsyncLogin().execute(username, password);
// .....
}
When a screen orientation change occurs, your activity is still destroyed. As a result a new AsyncTask is started. But when the old AsyncTask completes the job, it tries to deliver the result to the old activity, that was destroyed already. As a result this will cause a crash.
The solution would be to put the AsyncTask in the fragment itself. So that the instance to not be destroyed on orientation change.
See if this article can help you.

Categories

Resources