Save Current Fragment in onSaveInstanceState - android

I have one Activity that launches a Fragment. I open my Navigation Drawer and go to a second Fragment in that same Activity. I rotate the device and instead of remaining on that second Fragment, I'm brought back to the first one. How can you save the current Fragment that you're on so that when the Activity is destroyed it opens the appropriate Fragment? Here is what I have tried but it's not working. This is in my Activity:
private FragmentTransaction ft;
private Fragment fragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
fragment = getFragmentManager().getFragment(savedInstanceState,"currentFrag");
ft.replace(R.id.fragment_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}else{
fragment = new HomeFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
fragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment).commit();
}
}
.....
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getFragmentManager().putFragment(outState, "currentFrag", fragment);
}
This is the exception in my log cat
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentTransaction.replace(int, android.app.Fragment)' on a null object reference
Clearly the current Fragment isn't being saved. Is this generally the correct way to solve this?
This question has nothing to do with the general understanding of what a NullPointerException is but rather how to persist Fragment states when the screen is rotated.

In your fragment constructor, add the following line:
setRetainInstance(true);
Control whether a fragment instance is retained across Activity
re-creation (such as from a configuration change).

I don't see where you initialize your FragmentTransaction, sorry.
Maybe you miss this:
fragment = getFragmentManager().getFragment(savedInstanceState,"currentFrag");
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment);

Related

Fragment blank after popBackStack

I have this Activity in which I replace the main fragment with a preference fragment. When I click back after looking at the preferences, I get a blank (white) area where my fragment should be. If I rotate the screen then it works just fine. Everything in my fragment appears to be ok except for it is blank. Here are my methods:
The onCreate method of the activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(MAIN_CONTAINER, new MainFragment())
.commit();
}
}
The starting of the preferences fragment:
public void startPreferencesFragment() {
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
MyPreferencesFragment mPrefsFragment = new MyPreferencesFragment();
mFragmentTransaction.addToBackStack(null)
.replace(MAIN_CONTAINER, mPrefsFragment)
.commit();
}
The onBackPressed of my activity:
#Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
return;
}else {
super.onBackPressed();
}
}
So what am I doing wrong?
Thanks.
EDIT: If I open preferences and then rotate and then press back, it all works fine.
Also I should mention that removing the onBackPressed method does not fix the issue, it just exits the app.
EDIT: Turned out to not be a problem with the fragment back stack at all. Basically my fragment has a recyclerview and that is all it has. The instance of the adapter I was setting on the recyclerview was being kept while the recyclerview itself was new when the fragment was brought back from the back stack and I was checking whether the adapter was null when setting it.
You are already adding the transaction to the backstack, there is not need to override onBackPressed(); the framework will pop the Fragment out of the stack automatically when the back button is pressed. I am pretty sure that you are "double" popping the backstack.

Android - fragment onActivityCreated() not getting called after .replace

My main application has a blanck FrameLayout the reason for this is so that I can on the go, add and replace fragments to it...
I have had no issue with this until now. I have successfully added the first fragment and the onCreate gets called etc, then in my code using a button I replace the FrameLayout with my second fragment which seems to execute, however not showing? as onActivityCreated() does not get called.
My main application calls FrameLayout xml file known as send_activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
sendControlFragment sendControlFragment = new sendControlFragment();
peerItemFragment fragmentList = new peerItemFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_activity);
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
sendControlFragment controlFragment = new sendControlFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction.add(R.id.fragment_container, controlFragment).commit();
}
}
The above works perfectly...
The button which I call the second fragment, I got told to initiate a new FragmentTransaction which I have done, however this does not call the onActivityCreated() and skips to fragmentList.addPeers(mService.peerList);
This is great that it is calling the addPeers() method however without the onActivityCreated() then it crashes!
public void authenticateClick(View v) {
mService.peerSearch();
peerItemFragment peerFragment = new peerItemFragment();
FragmentTransaction fragmentTransactionauthen = fragmentManager.beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
fragmentTransactionauthen.replace(R.id.fragment_container, peerFragment);
fragmentTransactionauthen.addToBackStack(null);
// Commit the transaction
fragmentTransactionauthen.commit();
//pass peer list to fragment to display
fragmentList.addPeers(mService.peerList);
}
Can you please suggest a solution as .replace currently to my knowledge doesnt call the onActivityCreated() method...
Because i was using a listfragment I had to replace my listView id to the following, as this was causing fatal error!
android:id="#android:id/list"
it now works and replaces fragments.

Make a Fragment's Lifecycle Mimic Another

WHAT I HAVE
I have a Fragment called MainFragment. This is added from my MainActivity. Within that Fragment in on its onResume method I add another Fragment called SecondFragment like so.
#Override
public void onResume() {
super.onResume();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
SecondFragment secondFragment = (SecondFragment) fragmentManager.findFragmentByTag(SecondFragment.FRAG_TAG);
if (secondFragment == null)
secondFragment = SecondFragment.newInstance();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(mContainer.getId(), secondFragment, SecondFragment.FRAG_TAG).commit();
}
I then remove it like so.
#Override
public void onPause() {
super.onPause();
FragmentManager fm = getActivity().getSupportFragmentManager();
SecondFragment mhpf = (SecondFragment) fm.findFragmentByTag( SecondFragment.FRAG_TAG );
if ( mhpf != null )
fm.beginTransaction().remove( mhpf ).commit();
}
THE ISSUE
This all works well and good but it doesn't fit in with the lifecycle of the Fragment when things like orientation changes happen or when the user goes home and then opens the application up again.
WHY I'M DOING IT LIKE THIS AND NOT ADDING IT FROM THE ACTIVITY
This is part of an Android Library Module so I want it self contained. The user just has to add the MainFragment and then the SecondFragment is handled entirely by the MainFragment.
MY QUESTION
Is there a way to get my SecondFragment to behave like a normal Fragment without having to add it and remove it from and Activity?
Use getActivity().getChildFragmentManager(); in place of getActivity().getSupportFragmentManager();
If you are using Fragment inside Fragment.

GetFragmentManager.findFragmentByTag() returns null

getFragmentManager().beginTransaction()
.replace(R.id.graph_fragment_holder, new GraphFragment(), "GRAPH_FRAGMENT")
.commit();
getFragmentManager().beginTransaction()
.replace(R.id.list_fragment_holder, new ListFragment(), "LIST_FRAGMENT")
.commit();
//getFragmentManager().executePendingTransactions();
GraphFragment graphFragment = (GraphFragment) getFragmentManager().findFragmentByTag("GRAPH_FRAGMENT");
graphFragment.setData(data);
ListFragment listFragment = (ListFragment) getFragmentManager().findFragmentByTag("LIST_FRAGMENT");
listFragment.setData(data);
I've supplied a tag so I'm not sure why findFragmentByTag() returns null.
What I've tried from reading other questions:
this.setRetainInstance(true) in the oncreate of both fragments.
Both fragment constructors are empty public fragmentName(){}.
tried executePendingTransactions after adding the fragments.
tried add instead of replace on the fragments (edited)
I was confused about this for a long time. First, you need to save the fragment you are replacing by pushing it onto the back stack. The tag you supply is put on the fragment you are adding, not the one you are pushing onto the back stack. Later, when you do push it onto the back stack, that tag goes with it. Here's code with objects broken out to make it easier to trace. You must call 'addToBackStack' before 'commit'.
GraphFragment grFrag = new GraphFragment();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.replace(R.id.fragment_container, grFrag, "GRAPH_FRAGMENT");
// grFrag is about to become the current fragment, with the tag "GRAPH_FRAGMENT"
tr.addToBackStack(null);
// 'addToBackStack' also takes a string, which can be null, but this is not the tag
tr.commit();
// any previous fragment has now been pushed to the back stack, with it's tag
ListFragment liFrag = new ListFragment();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.replace(R.id.fragment_container, liFrag, "LIST_FRAGMENT");
// liFrag is is about to become the current fragment, with the tag "LIST_FRAGMENT"
tr.addToBackStack(null);
tr.commit();
// 'grFrag' has now been pushed to the back stack, with it's tag being "GRAPH_FRAGMENT"
Call getFragmentManager().executePendingTransactions() after fragment transaction.
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new ExampleFragment(), "YOUR TAG HERE");
.commit();
//after transaction you must call the executePendingTransaction
getFragmentManager().executePendingTransactions();
//now you can get fragment which is added with tag
ExampleFragment exampleFragment = getFragmentManager().findFragmentByTag("YOUR TAG HERE");
I was having the same problem of findFragmentByTag() always returning null.
Eventually I tracked it down, I was overriding onSaveInstanceState() in my Activity but not calling super. As soon as I fixed that findFragmentByTag() returned the Fragment as expected.
You can use
fragmentTransaction.addToBackStack(yourFragmentTag);
After that you can reuse it with
getSupportFragmentManager().findFragmentByTag(yourFragmentTag);
Answered here, just need to call getSupportFragmentManager().executePendingTransactions(); after your findByTag or findById
In my case I had to create a class level FragmentManager object and then use it instead of using getSupportFragmentManager() directly.
public class Main extends BaseActivity {
FragmentManager fragmentManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmain);
fragmentManager = getSupportFragmentManager();
initFrag1();
}
private void initFrag1() {
String name = Frag1.class.getSimpleName();
if (fragmentManager.findFragmentByTag(name) == null) {
fragmentManager.beginTransaction()
.add(R.id.frag_container, new Frag1(), name)
.addToBackStack(name)
.commit();
}
}
}

Retain the Fragment object while rotating

I have developed an app in Honeycomb and I am using fragments.
This is my app
I have an Activity (Say A1) and in that there is a fragment
Initially this fragment hold the object one fragment object say (F1)
Then depending on the user actions it may change to other objects F2,F3 ....
What my problem is
When The user rotate the device the activity is recreated and which make F1 as the fragment object even though before rotating it wasn't
What is the way to retain the fragment object while rotating?
I used setRetainInstance(true); but it didn't work for me
And I have added the fragment by code in my onCreate function like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Fragment homeFragment = new Home();
fragmentTransaction.add(R.id.mainFragement, homeFragment);
fragmentTransaction.commit();
}
By default Android will retain the fragment objects. In your code you are setting the homeFragment in your onCreate function. That is why it is allways some homeFragment or fl what ever that you set in onCreate.
Because whenever you rotate, the onCreate will execute and set your fragment object to the first one
So the easy solution for you is check whether savedInstanceState bundle is null or not and set the fragment object
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(null == savedInstanceState) {
// set you initial fragment object
}
}
You need to give your Fragment a unique tag, and check whether this Fragment is already added to your Activity already or not.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String tag = "my_fragment";
FragmentManager fragmentManager = getFragmentManager();
if(fragmentManager.findFragmentByTag(tag) == null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Fragment homeFragment = new Home();
fragmentTransaction.add(R.id.mainFragement, homeFragment, tag);
fragmentTransaction.commit();
}
}
Checking whether savedInstanceState is null is not a safe way to check whether your fragment is already set - it will work in most cases, but in some cases (such as when the device is on low memory), Android may kill your Activity, which could break your application.
To see this in action, tick "Don't keep activities" in the device's development options (the setting is available in Android 4.0+, not sure about earlier versions). When you open a new activity, your first activity is destroyed. When you return to it (by pressing back), it is created again, and savedInstanceState is not null. However, your fragment is not in the activity anymore, and you have to add it again.
EDIT - Showing the original principle but with SupportFragmentManager
public class ActivityAwesome extends AppCompatActivity
{
private final String TAG = getClass().getSimpleName();
private FragmentHome mHomeFragment;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(TAG);
if(fragment == null)
{
// Create the detail fragment and add it to the activity using a fragment transaction.
mHomeFragment = new FragmentHome();
fragmentManager.beginTransaction()
.add(R.id.fragment_container, mHomeFragment, TAG)
.commit();
}
else
{
// get our old fragment back !
mHomeFragment = (FragmentHome)fragment;
}
}
}
this comes in especially useful if you want to manipulate the fragment (in this case mHomeFragment) after rotating your device
Use onAttachFragment() in your Activity to reassign the object:
#Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof MyFragment)
this.myFragment = (MyFragment) fragment;
}
I defined a Fragment in activity's layout, onSaveInstanceState in the Fragment does get called, but the savedInstanceState Bundle in the Fragment's onCreatView comes as null.
The reason was that my Fragment did not have a ID in XML:
android:id="#+id/compass_fragment" ...
just rewiring #Ralf answer to be more dynamic, no need to specify a certain fragment to retain, but in case you want to specify, it is also possible :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set Home/Main/default fragment
changeFragmentTo(HomeFragment.newInstance(), FRAGMENT_TAG_HOME_FRAGMENT);
if (getCurrentFragment() != null) {
//if screen rotated retain Fragment
changeFragmentTo(getCurrentFragment(), getCurrentFragment().getTag());
}
}
private Fragment getCurrentFragment() {
//fl_main_container is FarmeLayout where I load my Fragments
return getSupportFragmentManager().findFragmentById(R.id
.fl_main_container);
}
/**
* changeFragmentTo(Fragment fragmentToLoad, String fragmentTag)
*
* #param fragmentToLoad : dataType > v4.app.Fragment :: the object of the fragment you want to load in form of MyFragment() or MyFragment().newInstance()
* #param fragmentTag : dataType > String :: a String which identify the "tag" of the fragment in form of "FRAGMENT_TAG_MY_FRAGMENT", Value must be stored in {#link models.MyConstants}
*/
public void changeFragmentTo(Fragment fragmentToLoad, String fragmentTag) {
if (getSupportFragmentManager().findFragmentByTag(fragmentTag) == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_main_container, fragmentToLoad, fragmentTag)
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(fragmentTag)
.commit();
} else {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_main_container, fragmentToLoad, fragmentTag)
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
}
}
You can simply set the RetainInstance property inside OnCreate of the fragment class.
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RetainInstance = true;
}
Retain the Fragment object while rotating

Categories

Resources