Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active December 20, 2015 11:29
Embed
What would you like to do?
Android Fragment
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import java.util.LinkedList;
public class BaseFragmentActivity extends Activity {
private LinkedList<FragmentTransaction> mFragmentTransactions;
private boolean mIsTransactionAllowed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragmentTransactions = new LinkedList<>();
}
@Override
protected void onStop() {
disableFragmentTransaction();
super.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
disableFragmentTransaction();
super.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
clearFragmentTransaction();
super.onDestroy();
}
@Override
protected void onPostResume() {
super.onPostResume();
allowFragmentTransaction();
commitPendingFragmentTransactions();
}
private void disableFragmentTransaction() {
mIsTransactionAllowed = false;
}
private void clearFragmentTransaction() {
mFragmentTransactions.clear();
}
private void allowFragmentTransaction() {
mIsTransactionAllowed = true;
}
private void commitPendingFragmentTransactions() {
while (!mFragmentTransactions.isEmpty()) {
FragmentTransaction fragmentTransaction = mFragmentTransactions.removeFirst();
fragmentTransaction.commit();
}
}
protected boolean commit(FragmentTransaction fragmentTransaction) {
if (mIsTransactionAllowed) {
fragmentTransaction.commit();
} else {
mFragmentTransactions.addLast(fragmentTransaction);
}
return true;
}
}
// Fragment
public class LoadingFragment extends Fragment {
private TaskCallbacks mTaskCallbacks;
private MyTask mTask;
public interface TaskCallbacks {
void onProgressUpdate(int percent);
}
public static LoadingFragment newInstance() {
LoadingFragment fragment = new LoadingFragment();
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof TaskCallbacks)) {
throw new IllegalArgumentException("Activity must implement TaskCallbacks");
}
mTaskCallbacks = (TaskCallbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mTaskCallbacks = null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mTask = new MyTask();
mTask.execute();
}
public void cancel() {
if(mTask != null) {
mTask.cancel(true);
}
}
private class MyTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i <= 100 && !isCancelled(); i++) {
SystemClock.sleep(100);
publishProgress(i);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
if (mTaskCallbacks != null) {
mTaskCallbacks.onProgressUpdate(values[0]);
}
}
}
}
// Activity
public class MyActivity extends Activity implements LoadingFragment.TaskCallbacks{
private LoadingFragment mTaskFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
L.LOG_TAG = "fragment lifecycle";
FragmentManager fm = getFragmentManager();
mTaskFragment = (LoadingFragment) fm.findFragmentByTag("task");
// If the Fragment is non-null, then it is currently being
// retained across a configuration change.
if (mTaskFragment == null) {
mTaskFragment = LoadingFragment.newInstance();
fm.beginTransaction().add(mTaskFragment, "task").commit();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTaskFragment != null) {
mTaskFragment.cancel();
}
}
@Override
public void onProgressUpdate(int percent) {
L.d("onProgressUpdate " + percent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment