Fixed fragments dissapearing on activity result
I overrided FeatureListFragment.onActivtyResult() so I could remove and reload the fragments. Also in FeatureListFragment.loadFragments() I removed the if block that replaces fragments. Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
@@ -64,15 +65,22 @@ public class FeatureListFragment extends Fragment {
|
||||
*/
|
||||
private void loadFragments() {
|
||||
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
||||
for(Fragment fragment : this.mFragments){
|
||||
if(!fragment.isAdded())
|
||||
transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
||||
else
|
||||
transaction.replace(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
||||
}
|
||||
for(Fragment fragment : this.mFragments)
|
||||
transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see android.support.v4.app.Fragment#onActivityResult(int, int, android.content.Intent)
|
||||
*/
|
||||
@Override
|
||||
public void onActivityResult(int arg0, int arg1, Intent arg2) {
|
||||
removeFragments();
|
||||
loadFragments();
|
||||
super.onActivityResult(arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater,
|
||||
@@ -90,16 +98,11 @@ public class FeatureListFragment extends Fragment {
|
||||
@Override
|
||||
public void onPause() {
|
||||
try{
|
||||
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
||||
for(Fragment fragment : this.mFragments){
|
||||
transaction.remove(fragment);
|
||||
}
|
||||
transaction.commitAllowingStateLoss();
|
||||
removeFragments();
|
||||
} catch(IllegalStateException e){
|
||||
e.printStackTrace();
|
||||
//do nothing
|
||||
}
|
||||
|
||||
Collections.reverse(this.mFragments);
|
||||
super.onPause();
|
||||
}
|
||||
@@ -113,15 +116,6 @@ public class FeatureListFragment extends Fragment {
|
||||
if (Debug.DEBUG)
|
||||
Log.v(TAG, "onResume()");
|
||||
loadFragments();
|
||||
|
||||
final View v = this.getView();
|
||||
if(v != null)
|
||||
v.post(new Runnable(){
|
||||
@Override
|
||||
public void run(){
|
||||
v.invalidate();
|
||||
}
|
||||
});
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@@ -136,4 +130,17 @@ public class FeatureListFragment extends Fragment {
|
||||
transaction.remove(fragment);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all fragments from the the view
|
||||
* @throws IllegalStateException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void removeFragments() throws IllegalStateException {
|
||||
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
||||
for(Fragment fragment : this.mFragments){
|
||||
transaction.remove(fragment);
|
||||
}
|
||||
transaction.commit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user