I still dont seem to get this State Loss error when commiting a fragment. I have a ListView with this OnClickListener:
public void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
fragmentTabActivity.addFragments(MainTabHostActivity.getTabNames()[0], new OpenerLocationListFragment(), true);
break;
}
}
And this is my addFragments Method:
public void addFragments(String tabName, Fragment fragment, boolean add) {
if (add) {
hMapTabs.get(tabName).add(fragment);
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment);
ft.commit();
}
How can it lose his state in this short time frame? I can understand why it happens in Asynctasks but what should i do different here (other than allowing the state loss)? Has anybody suggestions?
Related
I currently have a navigation drawer, which has a few fragments (Home, Help, About) in my activity. On startup it opens up Home. The issue i'm having is that when i go to another fragment such as Help and then proceed to put the phone to sleep and subsequently turn on the phone back on it'll always return to Home instead of help.
I'm quite new to lifecycles but was hoping to get some feedback on how to resume from a different fragment.
EDIT: Provided relevant code
Update: Realised that this happens because i reinit the views on resume.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI() {
fragAbout = new About();
fragHelp = new Help();
fragHome = new MyViewPager();
// Adding fragments to activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_activity_fraglayout, fragHome);
transaction.commit();
...
}
private void addDrawerItems() {
...
DrawerItemAdapter drawerAdapter = new DrawerItemAdapter(this, R.layout.nav_list_row, drawerItems);
mDrawerList.setAdapter(drawerAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
...
newFragOnClick(fragHome, "Home");
break;
case 1:
...
newFragOnClick(fragSettings, "Help");
break;
case 2:
...
newFragOnClick(fragAbout, "About");
break;
default:
break;
}
}
});
}
private void newFragOnClick(Fragment frag, String actionBarTitle){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_activity_fraglayout, frag);
transaction.commit();
}
Use sharedpreferences to save the current tab position and in onResume() use it to move to the saved position.
I'm currently trying to programmatically add a fragment which has a view that I need to initializate immediately after the fragment is created. I tried to use FragmentManager.executePendingTransactions() right after FragmentTransition.commit(), but it doesn't work as it supposed to. I've made a research and found this, so I realized that FragmentManager.executePendingTransactions() doesn't work from onCreate() and then put code to onStart(), but it still doesn't work. Although in my case it's called from onItemClickListener(), I don't think it makes any difference. Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vk_music_list);
musicProgressBar = new MusicProgressBar();
listView = (ListView) findViewById(R.id.listView);
}
#Override
protected void onStart() {
super.onStart();
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
serviceIntent.putExtra("audioUrl", audioUrl);
//Inflate music progress bar
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
if(fragment == null) {
fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
.setTransition(R.anim.move_from_the_bottom)
.commit();
}
//This guy doesn't work
fragmentManager.executePendingTransactions();
//Never get into this, because fragment is always == null
if(fragment != null) {
SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
}
startService(serviceIntent);
}
Alright, I finally found out what the issue is. The reason of the problem is that neither fragmentTransaction.commit() nor fragmentManager.executePendingTransactions() change reference that is stored in fragment variable. Therefore, after executePendingTransactions() I need to call findFragmentById() again, so reference that it stores will refer to the created fragment. Here's a working code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
serviceIntent.putExtra("audioUrl", audioUrl);
//Inflate music progress bar
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
if(fragment == null) {
fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
.commit();
}
//Force to create fragment without scheduling so it's not necessarily to wait until it get scheduled
fragmentManager.executePendingTransactions();
//This is what I needed
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
//Set song duration as a seekBar's maximum
if (fragment != null) {
SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
}
startService(serviceIntent);
}
I hope it will be helpful for somebody.
In my navigation drawer I have to selections: 1)Laptop 2)Desktop
How can I open another fragment when I click on Desktop? Sorry, I am new in Android Programming
I have also visited the other questions about this in stackoverflow, but they still do not work
Thanks
public void onNavigationDrawerItemSelected(int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position){
case 0:
fragment = new FragmentLaptop();
break;
case 1:
fragment = new FragmentDesktop();
break;
}
transaction
.replace(R.id.container, fragment)
.commit();
}
onNavigationDrawerItemSelected(position)needs to be called in onItemClick(...) where you have the listener of the list
like this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
onNavigationDrawerItemSelected(position);
}
My ERROR :
java.lang.IllegalStateException: commit already called
My CODE:
final FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
f1_fragment = new F1_Fragments();
f2_fragment = new F2_Fragments();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
if(position==0){
fragmentTransaction.replace(android.R.id.content, f1_fragment);
}else{
fragmentTransaction.replace(android.R.id.content, f2_fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
You are beginning the FragmentTransaction outside of the OnItemClickListener. Thus you are attempting to commit() a single FragmentTransaction every time the user clicks an item in your ListView.
You need to begin a new FragmentTransaction every time you intend to perform any number of Fragment operations.
A simple fix would look like this:
f1_fragment = new F1_Fragments();
f2_fragment = new F2_Fragments();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
parent.getItemAtPosition(position);
if(position==0){
fragmentTransaction.replace(android.R.id.content, f1_fragment);
}else{
fragmentTransaction.replace(android.R.id.content, f2_fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
you can use this method for replace fragment with each other just call this
do those are global
YourFragment1 frg1 = new YourFragment1 ();
YourFragment2 frg1 = new YourFragment2 ();
And then call it by
openFragment(frg1);
or
openFragment(frg2);
OpenFragment:
private void openFragment(final Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Look at this scenario
I need to add new fragment on every list item click. Just initialise fragment transaction on every item click.
switch (v.getId()) {
case R.id.tvaddExpense:
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.containerFucntionsList, new Fragment1());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.relEvents:
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerFucntionsList, new Fragment2());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
}
I had same issue and I have solved by creating new instance of FragmentTransaction.
Just add everytime below line before add / replace fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Hope this would help you.
The Error
java.lang.IllegalStateException: commit already called
shows that the FragmentTransaction has been completed after calling commit() the first time and you are again calling commit() which tends to complete it once again. Hence it makes an Illegal state for the FragmentTransaction.
As per your code, you are using the same FragmentTransaction for changing fragments. However, after the first commit() call, the FragmentTransaction has completed and you need to begin it again to perform any operation on Fragments.
You can change your ClickListner as:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
parent.getItemAtPosition(position);
if(position==0){
fragmentTransaction.replace(android.R.id.content, f1_fragment);
}else{
fragmentTransaction.replace(android.R.id.content, f2_fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
I hope it clears your doubt.
You must define a FragmentTransAction object separately each time you add a fragment:
switch (position){
case 0:
FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.parentOfMenu, new FragmentHome());
fragmentTransaction1.commit();
break;
case 1:
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.parentOfMenu, new FragmentMedia());
fragmentTransaction2.commit();
break;
case 2:
FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.parentOfMenu, new FragmentChart());
fragmentTransaction3.commit();
break;
case 3:
FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.parentOfMenu, new FragmentProfile());
fragmentTransaction4.commit();
break;
}
I solved the issue
after calling commit() and again replacing the fragment you should start from
fragmentTransaction = fragmentManager.beginTransaction();
if(position==0){
fragmentTransaction.replace(android.R.id.content, f1_fragment);
else{
fragmentTransaction.replace(android.R.id.content, f2_fragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I am implementing navigation drawer and works well. So i am calling fragment on navigation drawer click and it is also working and further more i am calling another fragment from Home page fragment and maintain the back stack for every fragment but the problem is back press from the child fragment i can't go to Home page fragment and just exited from application. I don't want this. What i want Click on
Navigation Drawer->HomePageFragment->AnotherChild Fragment(On List Item click of HomePageFragment)
but on back pressed without going to Homepage fragment its directly exit with application. Here is my code: (In Fragment Activity with Navigation Drawer)
class SlideitemListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
updateDisplay(position);
}
}
private void updateDisplay(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new Result_Fragment();
break;
case 2:
fragment = new Live_Match_Fragment();
break;
case 3:
// fragment = new Live_Match_Fragment();
break;
case 4:
fragment = new Team_Fragment();
break;
default:
break;
}
if (fragment != null) {
fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate("0", 0);
int count = fragmentManager.getBackStackEntryCount();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(String.valueOf(count)).commit();
Log.e("Count in Activiy", ""+count);
// update selected item and title, then close the drawer
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
Now in BackPressed() in FragmentActivity.
#Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() <= 1) {
finish();
return;
}
super.onBackPressed();
}
Now calling another child fragment from HomePage fragment on Listview item click.
team_lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TeamDetailFragment myDetailFragment = new TeamDetailFragment();
FragmentManager fragmentManager = getFragmentManager();
int count = fragmentManager.getBackStackEntryCount();
Log.e("Count in Fragment", "" + count);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction
.replace(R.id.frame_container, myDetailFragment)
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(String.valueOf(count)).commit();
}
});
So anybody knows then help me. Help will be appreciate.
There is an issue with nested fragments in android
https://code.google.com/p/android/issues/detail?id=40323
Android doesn't handle back press well if transactions were in nested fragments.
To surpass this i am using the following fix inside My Activity
#Override
public void onBackPressed() {
// If the fragment exists and has some back-stack entry
if (myFragment != null && myFragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
// Get the fragment fragment manager - and pop the backstack
myFragment.getChildFragmentManager().popBackStack();
}
// Else, nothing in the direct fragment back stack
else {
// Let super handle the back press
super.onBackPressed();
}
}
I've solved in the following way:
1st solution
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getFragmentManager();
int backCount = fragmentManager.getBackStackEntryCount();
if(backCount > 1) {
super.onBackPressed();
} else {
finish();
}
}
2nd solution
#Override
public void onBackStackChanged() {
FragmentManager fragmentManager = getFragmentManager();
int backCount = fragmentManager.getBackStackEntryCount();
if(backCount == 0) {
finish();
}
}
These allow you to exploit the Android stack when you replace your fragment:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, yourFragment)
.addToBackStack(null).commit();
When you press back button inside your fragment onBackPressed() method of your activity will be called if you have declared that..So handling back button for fragments within navigation drawer can be one in this way..
MainActvity
public static boolean isMainActivityShown ;
public static boolean isFragment1Shown=false ;
public static boolean isFragment2Shown=false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isMainActivityShown=true //inside onCreate method put isMainActivityShown true
.
.
.
{
Fragment currentFragment = new Fragment1();
isMainActivityShown=false; //when moving to fragment1
isFragment1Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
}
.
.
}
#Override
public void onBackPressed() {
if(isMainActivityShown)
{
finish();
}
else if(isFragment1Shown)
{
//write the code to handle back button when you are in Fragment1
}
else if(isFragment2Shown)
{ //When you are in Fragment 2 pressing back button will move to fragment1
Fragment currentFragment = new Fragment1();
isFragment2Shown=false;
isFragment1Shown=true;
FragmentManager frgManager;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
}
}
Fragment1
Fragment currentFragment = new Fragment2();
MainActivity.isFragment1Shown=false;
MainActivity.isFragment2Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
Now you can replace finish() for fragmentManager.popBackStack(), method of android.support.v4.app.FragmentManager