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 java.util.Collections;
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
@@ -64,15 +65,22 @@ public class FeatureListFragment extends Fragment {
|
|||||||
*/
|
*/
|
||||||
private void loadFragments() {
|
private void loadFragments() {
|
||||||
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
||||||
for(Fragment fragment : this.mFragments){
|
for(Fragment fragment : this.mFragments)
|
||||||
if(!fragment.isAdded())
|
|
||||||
transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
||||||
else
|
|
||||||
transaction.replace(R.id.fragment_list_contianer, fragment, fragment.getTag());
|
|
||||||
}
|
|
||||||
transaction.commit();
|
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)
|
* (non-Javadoc)
|
||||||
* @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater,
|
* @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater,
|
||||||
@@ -90,16 +98,11 @@ public class FeatureListFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
try{
|
try{
|
||||||
final FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
|
removeFragments();
|
||||||
for(Fragment fragment : this.mFragments){
|
|
||||||
transaction.remove(fragment);
|
|
||||||
}
|
|
||||||
transaction.commitAllowingStateLoss();
|
|
||||||
} catch(IllegalStateException e){
|
} catch(IllegalStateException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
//do nothing
|
//do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.reverse(this.mFragments);
|
Collections.reverse(this.mFragments);
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
@@ -113,15 +116,6 @@ public class FeatureListFragment extends Fragment {
|
|||||||
if (Debug.DEBUG)
|
if (Debug.DEBUG)
|
||||||
Log.v(TAG, "onResume()");
|
Log.v(TAG, "onResume()");
|
||||||
loadFragments();
|
loadFragments();
|
||||||
|
|
||||||
final View v = this.getView();
|
|
||||||
if(v != null)
|
|
||||||
v.post(new Runnable(){
|
|
||||||
@Override
|
|
||||||
public void run(){
|
|
||||||
v.invalidate();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
super.onResume();
|
super.onResume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,4 +130,17 @@ public class FeatureListFragment extends Fragment {
|
|||||||
transaction.remove(fragment);
|
transaction.remove(fragment);
|
||||||
transaction.commit();
|
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