I'm using BottomNavigationView in android to make a application just like Instagram. I'm using fragments with the navigationTabs. App have 5 tabs initialy I've set the middle tab as active tab and loads it once the app start. when i click on any other tab a network call is made and data is loaded. Now when i press on back button or click on the last tab again(which was loaded on startup) the fragment is recreated and the network call is made to load the same data. I want to show the previous fragments with same data without recreating.
I've tried using
transaction.add(container,fragment);
but to no avail.
my code on tab click
if (item.getItemId() == R.id.nav_OverView && _current != R.id.nav_OverView) {
Overview _overView = new Overview();
_fragmentTransaction.hide(_currentFragment);
_fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
_fragmentTransaction.add(R.id.content_base_drawer, _overView);
_fragmentTransaction.commit();
_current = R.id.nav_OverView;
viewIsAtHome = true;
}
I know using remove and add is same as using replace.
Any help is appreciated.
Before creating the view you can check if the view is already created or not, the below code helps fragment recreating problem.
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if (view == null)
{
view = inflater.inflate(R.layout.frag_layout, container, false);
init(view);
}
return view;
}
Make _overview into a class field (instead of a local variable), and change your listener code to this:
Also, use replace instead of hide+add, this will prevent Fragment already added errors.
Replace an existing fragment that was added to a container. This is
essentially the same as calling remove(Fragment) for all currently
added fragments that were added with the same containerViewId and then
add(int, Fragment, String) with the same arguments given here.
See example:
if (item.getItemId() == R.id.nav_OverView && _current != R.id.nav_OverView) {
if (_overView == null) {
_overView = new Overview();
}
_fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
_fragmentTransaction.replace(R.id.content_base_drawer, _overView);
_fragmentTransaction.commit();
_current = R.id.nav_OverView;
viewIsAtHome = true;
}
Related
EDIT:
After some more tinkering around, I found out the reason for my previous problem (see below. TL;DR: I'm trying to pass a Bundle from an activity to its fragment by replacing the fragment) is that when replacing fragments like this:
AddEditActivityFragment fragment = new AddEditActivityFragment();
Bundle arguments = getIntent().getExtras();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.add_edit_fragment, fragment)
.commit();
the replaced fragment does not get destroyed (as it should be according to a bunch of sources), which I verified by overriding and adding logging to onPause, onStop, onDestroyView, onDestroy, etc. None of them are called.
Calling popBackStackImmediate also does nothing.
What can I do?
Previous question title:
"Setting FloatingActionButton icon dynamically with setImageDrawable doesn't have any effect."
I have a FAB inside a fragment (called AddEditFragment), that serves to save user input to database.
To differentiate between editing rows from the DB and creating new ones, I had its icon set either a "send" icon or a "save" icon.
By default, the button is set to "send":
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_edit_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/add_edit_toolbar"
app:layout_anchorGravity="bottom|right|end"
app:srcCompat="#drawable/ic_send_white_24dp"/>
I used to set the icon to "save" by implementing an interface in my fragment, which serves to pass data from the containing activity to the fragment, using this method:
#Override
public void receiveData(Serializable data) {
Log.d(TAG, "receiveData: called");
mFoodItem = (FoodItem) data;
if (mFoodItem != null) {
mEditMode = true;
fab.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_save_white_24dp));
utilDisplayFoodItem();
}
}
The call the setImageDrawable worked fine and changed the icon from "send" to "save" properly.
Here is when I run into trouble.
I am trying to remove my AddEditFragment class' dependency on my AddEditActivity class, by removing said interface implementation and passing the data required by the fragment via Bundle:
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: starts");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit);
AddEditActivityFragment fragment = new AddEditActivityFragment();
Bundle arguments = getIntent().getExtras();
boolean editMode = arguments != null;
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.add_edit_fragment, fragment)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
getSupportActionBar().setTitle((editMode) ? "Edit Item:" : "Create Item:");
getSupportActionBar().setElevation(0);
Log.d(TAG, "onCreate: ends");
}
In this context, I removed receiveData method from the fragment, and placed this line:
fab.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_save_white_24dp));
in various likely places in my fragment class (the likeliest being inside its onCreateView method).
It doesn't seem to have any effect. My cases are:
Just adding said setImageDrawable call - sets icon to "save" in both add/edit modes, but then I have no "send" icon.
Setting either case dynamically (setImageDrawable call is inside if block) - icon is set to "send" in both add/edit modes.
Removing default "send" icon from XML, then setting either case dynamically - icon is set to "send" in both add/edit modes.
Removing default icon from XML, then setting only to "save" dynamically (no if block) - sets icon to "save" in both add/edit modes, but then I have no "send" icon.
It seems the setImageDrawable call, which worked perfectly in my receiveData interface method, doesn't have any effect (at least when an icon is already set, or when inside if block).
I'm at a loss and would appreciate any help!
In reply to #ColdFire:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: starts");
View view = inflater.inflate(R.layout.fragment_add_edit, container, false);
ButterKnife.bind(this, view);
Bundle arguments = getArguments();
if (arguments != null) {
mFoodItem = (FoodItem) arguments.getSerializable(FoodItem.class.getSimpleName());
if (mFoodItem != null) {
mEditMode = true;
// Tried calling setImageDrawable here
}
}
// Tried calling setImageDrawable here
if (!mEditMode) {
// If adding an item, initialize it for right now's date and time
Calendar now = Calendar.getInstance();
mFoodItem.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
mFoodItem.setTime(now.getTimeInMillis() / Constants.MILLISECONDS);
}
utilDisplayFoodItem();
utilSetOnClickListeners();
setHasOptionsMenu(true);
// Tried calling setImageDrawable here
Log.d(TAG, "onCreateView: ends");
return view;
}
I should mention that everything else that depends on the Bundle data works correctly.
You might want to clear back stack by using the following method
private void clearBackStack() {
FragmentManager manager = getSupportFragmentManager();
if(manager.getBackStackEntryCount() > 0) {
FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
manager.popBackStackImmediate(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
You might want to check this post, Thanks
This will only destroy the current fragment to be replaced. If you want to destroy all you might want to read this stack question
So I have an activty with 3 tabs which I navigate between through the ActionBar (SupportActionBar). Each of the tabs has a Fragment with a WebView attached to them, with a TabListener implement as examplified in the Android Docs.
This all works fine, except that the onCreateView method of the Fragment is called each time a fragment is reattached. This in turn causes the WebView to either (1) be blank or (2) reload, if I call restoreState() on it (which I previously have saved manually).
I don't want the page to reload each time the user switches tabs. Neither do I want the scrollbar to reset. Or the HTML forms (if any) to be reset. How can I accomplish this?
Solved by keeping a reference to the WebView in the fragment class, and in onCreateView I do this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mWebView == null) {
mWebView = new WebView(container.getContext());
mWebView.restoreState(savedInstanceState);
}
else {
View parent = mWebView.getParent();
if (parent != null && parent instanceof ViewGroup)
((ViewGroup) parent).removeView(mWebView);
}
return mWebView;
}
I have an app with hierarchy like this:
FragmentTabHost (Main Activity)
- Fragment (tab 1 content - splitter view)
- Fragment (lhs, list)
- Framment (rhs, content view)
- Fragment (tab 2 content)
- Fragment (tab 2 content)
All fragment views are being inflated from resources.
When the app starts everything appears and looks fine. When I switch from the first tab to another tab and back again I get inflate exceptions trying to recreate tab 1's views.
Digging a little deeper, this is what's happening:
On the first load, inflating the splitter view causes its two child fragments to be added to the fragment manager.
On switching away from the first tab, it's view is destroyed but it's child fragments are left in the fragment manager
On switching back to the first tab, the view is re-inflated and since the old child fragments are still in the fragment manager an exception is thrown when the new child fragments are instantiated (by inflation)
I've worked around this by removing the child fragments from the fragment manager (I'm using Mono) and now I can switch tabs without the exception.
public override void OnDestroyView()
{
var ft = FragmentManager.BeginTransaction();
ft.Remove(FragmentManager.FindFragmentById(Resource.Id.ListFragment));
ft.Remove(FragmentManager.FindFragmentById(Resource.Id.ContentFragment));
ft.Commit();
base.OnDestroyView();
}
So I have a few questions:
Is the above the correct way to do this?
If not, how should I be doing it?
Either way, how does saving instance state tie into all of this so that I don't lose view state when switching tabs?
I'm not sure how to do this in Mono, but to add child fragments to another fragment, you can't use the FragmentManager of the Activity. Instead, you have to use the ChildFragmentManager of the hosting Fragment:
http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager()
The main FragmentManager of the Activity handles your tabs.
The ChildFragmentManager of tab1 handles the split views.
OK, I finally figured this out:
As suggested above, first I changed the fragment creation to be done programatically and had them added to the child fragment manager, like so:
public override View OnCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstance)
{
var view = inflater.Inflate(Resource.Layout.MyView, viewGroup, false);
// Add fragments to the child fragment manager
// DONT DO THIS, SEE BELOW
var tx = ChildFragmentManager.BeginTransaction();
tx.Add(Resource.Id.lhs_fragment_frame, new LhsFragment());
tx.Add(Resource.Id.rhs_fragment_frame, new RhsFragment());
tx.Commit();
return view;
}
As expected, each time I switch tabs, an extra instance of Lhs/RhsFragment would be created, but I noticed that the old Lhs/RhsFragment's OnCreateView would also get called. So after each tab switch, there would be one more call to OnCreateView. Switch tabs 10 times = 11 calls to OnCreateView. This is obviously wrong.
Looking at the source code for FragmentTabHost, I can see that it simply detaches and re-attaches the tab's content fragment when switching tabs. It seems the parent Fragment's ChildFragmentManager is keeping the child fragments around and automatically recreating their views when the parent fragment is re-attached.
So, I moved the creation of fragments to OnCreate, and only if we're not loading from saved state:
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (savedInstanceState == null)
{
var tx = ChildFragmentManager.BeginTransaction();
tx.Add(Resource.Id.lhs_fragment_frame, new LhsFragment());
tx.Add(Resource.Id.rhs_fragment_frame, new RhsFragment());
tx.Commit();
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstance)
{
// Don't instatiate child fragments here
return inflater.Inflate(Resource.Layout.MyView, viewGroup, false);
}
This fixed the creation of the additional views and switching tab's basically worked now.
The next question was saving and restoring view state. In the child fragments I need to save and restore the currently selected item. Originally I had something like this (this is the child fragment's OnCreateView)
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
var view = inflater.Inflate(Resource.Layout.CentresList, container, false);
// ... other code ommitted ...
// DONT DO THIS, SEE BELOW
if (savedInstance != null)
{
// Restore selection
_selection = savedInstance.GetString(KEY_SELECTION);
}
else
{
// Select first item
_selection =_items[0];
}
return view;
}
The problem with this is that the tab host doesn't call OnSaveInstanceState when switching tabs. Rather the child fragment is kept alive and it's _selection variable can be just left alone.
So I moved the code to manage selection to OnCreate:
public override void OnCreate(Bundle savedInstance)
{
base.OnCreate(savedInstance);
if (savedInstance != null)
{
// Restore Selection
_selection = savedInstance.GetString(BK_SELECTION);
}
else
{
// Select first item
_selection = _items[0];
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
// Don't restore/init _selection here
return inflater.Inflate(Resource.Layout.CentresList, container, false);
}
Now it all seems to be working perfectly, both when switching tabs and changing orientation.
Info: I have a 2 pane layout (2 child Fragments) inside a ParentFragment, which, of course, is inside a FragmentActivity. I have setRetainInstance(true) on the ParentFragment. On orientation change, the left child fragment doesn't get destroyed (onCreate() doesn't get called), which is normal (because of the parent retaining its instance).
Problem: On orientation change, the right fragment gets destroyed (onCreate() gets called). Why the hell is the right fragment destroyed and the left one isn't ?
EDIT: If I remove setRetainInstance(true), then the left fragment's onCreate() gets called twice (lol wtf) and the right fragment's onCreate() gets called once. So this isn't good either...
Code below for the ParentFragment:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
setHasOptionsMenu(true);
if (getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left) == null ||
!getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left).isInLayout())
{
if (mPresentationsListFragment == null)
mPresentationsListFragment = PresentationsListFragment.newInstance(PresentationsListFragment.TYPE_SCHEDULE, mScheduleDate);
getChildFragmentManager().beginTransaction()
.replace(R.id.fragment_schedule_framelayout_left, mPresentationsListFragment)
.commit();
}
mPresentationsListFragment.setOnPresentationClickListener(this);
return view;
}
#Override
public void onPresentationClick(int id)
{
if (Application.isDeviceTablet(getActivity()))
{
if (getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_right) == null)
{
if (mPresentationDetailFragment == null)
mPresentationDetailFragment = PresentationDetailFragment.newInstance(id);
else
mPresentationDetailFragment.loadPresentation(id);
getChildFragmentManager().beginTransaction()
.replace(R.id.fragment_schedule_framelayout_right, mPresentationDetailFragment)
.commit();
}
else
mPresentationDetailFragment.loadPresentation(id);
}
else
{
Intent presentationDetailIntent = new Intent(getActivity(), PresentationDetailActivity.class);
presentationDetailIntent.putExtra(PresentationDetailActivity.KEY_PRESENTATION_ID, id);
startActivity(presentationDetailIntent);
}
}
LE Solution:
Thanks a lot to antonyt , the answer is below. The only changes needed to pe performed reside inside onCreateView() of the parent Fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
setHasOptionsMenu(true);
if (getChildFragmentManager().findFragmentById(R.id.fragment_presentations_framelayout_left) == null)
{
mPresentationsListFragment = PresentationsListFragment.newInstance();
mPresentationsListFragment.setOnPresentationClickListener(this);
getChildFragmentManager().beginTransaction()
.add(R.id.fragment_presentations_framelayout_left, mPresentationsListFragment)
.commit();
}
return view;
}
From what I understand, if you have setRetainInstance(true) on the parent fragment with the above code, your left fragment should be recreated but your right fragment should not be, when changing orientation. This is backwards to what you wrote above, but I will explain why this is the case anyway. If you have setRetainInstance(false) on the parent fragment, you indeed should see the left fragment being created twice and the right fragment being created once.
Case 1: setRetainInstance(true)
Your parent fragment will not be destroyed on rotation. However, it will still recreate its views each time (onDestroyView and onCreateView will be called, in that order). In onCreateView you have code to add your left fragment under certain conditions. getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left) should be non-null, since a fragment was added to that container previously. getChildFragmentManager().findFragmentById(R.id.fragment_schedule_framelayout_left).isInLayout() should be false since only fragments added via XML will cause it to return true. The overall condition is true and so a new instance of your left fragment will be created and it will replace the old one. Your right fragment is only instantiated during a click event and so no special behavior happens.
Summary: Parent fragment remains, new left fragment is created, right fragment remains.
Case 2: setRetainInstance(false)
Your parent fragment is destroyed, and so are the left and right fragments. All three fragments are recreated automatically by Android. Your parent fragment will then get a chance to create its view, and it will create a new instance of the left fragment as per the explanation above. The just-created left fragment will be replaced by this new instance. You will observe that a left fragment will be destroyed and another left fragment will be created. No special behavior happens for the right fragment.
Summary: New parent fragment is created, two new left fragments are created, new right fragment is created.
If you are sure that in the setRetainInstance(true) case, your right fragment is being destroyed and not your left one, please post a sample project to github/etc. that demonstrates this.
Update: Why the right fragment gets removed if you use FragmentTransaction.replace() on the left fragment
Because of the inner conditional, your code will try to replace your left fragment with itself on the same container.
Here is the code snippet from the Android 4.1 source code that handles a replace:
...
case OP_REPLACE: {
Fragment f = op.fragment;
if (mManager.mAdded != null) {
for (int i=0; i<mManager.mAdded.size(); i++) {
Fragment old = mManager.mAdded.get(i);
if (FragmentManagerImpl.DEBUG) Log.v(TAG,
"OP_REPLACE: adding=" + f + " old=" + old);
if (f == null || old.mContainerId == f.mContainerId) {
if (old == f) {
op.fragment = f = null;
} else {
if (op.removed == null) {
op.removed = new ArrayList<Fragment>();
}
op.removed.add(old);
old.mNextAnim = op.exitAnim;
if (mAddToBackStack) {
old.mBackStackNesting += 1;
if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Bump nesting of "
+ old + " to " + old.mBackStackNesting);
}
mManager.removeFragment(old, mTransition, mTransitionStyle);
}
}
}
}
if (f != null) {
f.mNextAnim = op.enterAnim;
mManager.addFragment(f, false);
}
} break;
...
If you try to replace the same fragment with itself, there is some code to try and ignore this operation:
if (old == f) {
op.fragment = f = null;
}
Since f is null, and we are still continuing to iterate through our fragments, this seems to have the side effect of removing every subsequent fragment from the FragmentManager. I don't think this is intentional, but at the very least explains why your right fragment is getting destroyed. Not using replace / not replacing the same fragment with itself can fix your issues.
Interestingly, this was a recent change and did not exist in previous versions of Android.
https://github.com/android/platform_frameworks_support/commit/5506618c80a292ac275d8b0c1046b446c7f58836
Bug report: https://code.google.com/p/android/issues/detail?id=43265
I'm currently testing my app with a multipane Fragment-ised view using the HC compatibility package, and having a lot of difficultly handling orientation changes.
My Host activity has 2 panes in landscape (menuFrame and contentFrame), and only menuFrame in portrait, to which appropriate fragments are loaded. If I have something in both panes, but then change the orientation to portrait I get a NPE as it tries to load views in the fragment which would be in the (non-existent) contentFrame. Using the setRetainState() method in the content fragment didn't work. How can I sort this out to prevent the system loading a fragment that won't be shown?
Many thanks!
It seems that the onCreateViewMethod was causing issues; it must return null if the container is null:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) // must put this in
return null;
return inflater.inflate(R.layout.<layout>, container, false);
}
Probably not the ideal answer but if you have contentFrame for portrait and in your activity only load up the menuFrame when the savedInstanceState is null then your content frame fragments will be shown on an orientation change.
Not ideal though as then if you hit the back button (as many times as necessary) then you'll never see the menu fragment as it wasn't loaded into contentFrame.
It is a shame that the FragmentLayout API demos doesn't preserve the right fragment state across an orientation change. Regardless, having thought about this problem a fair bit, and tried out various things, I'm not sure that there is a straightforward answer. The best answer that I have come up with so far (not tested) is to have the same layout in portrait and landscape but hide the menuFrame when there is something in the detailsFrame. Similarly show it, and hide frameLayout when the latter is empty.
Create new Instance only for First Time.
This does the trick:
Create a new Instance of Fragment when the
activity start for the first time else reuse the old fragment.
How can you do this?
FragmentManager is the key
Here is the code snippet:
if(savedInstanceState==null) {
userFragment = UserNameFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.profile, userFragment, "TAG").commit();
}
else {
userFragment = fragmentManager.findFragmentByTag("TAG");
}
Save data on the fragment side
If your fragment has EditText, TextViews or any other class variables
which you want to save while orientation change. Save it
onSaveInstanceState() and Retrieve them in onCreateView() method
Here is the code snippet:
// Saving State
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("USER_NAME", username.getText().toString());
outState.putString("PASSWORD", password.getText().toString());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_name_fragment, parent, false);
username = (EditText) view.findViewById(R.id.username);
password = (EditText) view.findViewById(R.id.password);
// Retriving value
if (savedInstanceState != null) {
username.setText(savedInstanceState.getString("USER_NAME"));
password.setText(savedInstanceState.getString("PASSWORD"));
}
return view;
}
You can see the full working code HERE