Last active
November 18, 2017 10:34
-
-
Save Tenkei/04d15d6fa743bebaf6b02f8f156e41c1 to your computer and use it in GitHub Desktop.
How to Perform Fragment Transactions to avoid Activity.onSaveInstanceState Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import java.util.ArrayDeque; | |
import java.util.Queue; | |
public class MainActivity extends AppCompatActivity { | |
private boolean isRunning; | |
private Queue<FragmentTransaction> mFragmentTransactions = new ArrayDeque<>(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setupBaseFragment(); | |
} | |
@Override | |
protected void onPostResume() { | |
super.onPostResume(); | |
isRunning = true; | |
if(mFragmentTransactions != null) | |
while(!mFragmentTransactions.isEmpty()) | |
mFragmentTransactions.remove().commit(); | |
} | |
@Override | |
protected void onStop() { | |
isRunning = false; | |
super.onStop(); | |
} | |
public void setupBaseFragment(){ | |
//Find Active Fragment if Available | |
Fragment baseFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); | |
//Create Fragment if no Fragment is Available | |
if(baseFragment == null) | |
baseFragment = new HomeFragment(); | |
setFragment(baseFragment, false); | |
} | |
public void setFragment(Fragment fragment, boolean addToBackStack){ | |
if(fragment == null) | |
return; | |
//Create New Transaction | |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | |
ft.replace(R.id.fragment_container, fragment, fragment.getClass().getCanonicalName()); | |
if(addToBackStack) | |
ft.addToBackStack(fragment.getClass().getCanonicalName()); | |
//Commit Transaction if Activity is running else wait for Activity to Resume | |
if(isRunning){ | |
ft.commit(); | |
} else { | |
mFragmentTransactions.add(ft); | |
} | |
} | |
public void addFragmentOnTop(Fragment fragment, boolean addToBackStack){ | |
if(fragment == null) | |
return; | |
//Create New Transaction | |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | |
ft.add(R.id.fragment_container, fragment, fragment.getClass().getCanonicalName()); | |
if(addToBackStack) | |
ft.addToBackStack(fragment.getClass().getCanonicalName()); | |
//Commit Transaction if Activity is running else wait for Activity to Resume | |
if(isRunning){ | |
ft.commit(); | |
} else { | |
mFragmentTransactions.add(ft); | |
} | |
} | |
public Fragment getFragment(String fragmentTag){ | |
return getSupportFragmentManager().findFragmentByTag(fragmentTag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment