Skip to content

Instantly share code, notes, and snippets.

View noaht11's full-sized avatar

Noah Tajwar noaht11

View GitHub Profile
@noaht11
noaht11 / OC-ConfigChangesFlag.xml
Last active August 29, 2015 14:27
Using the android:configChanges flag on an activity in AndroidManifest.xml
<activity
android:name=".MyActivity"
android:label="@string/title_my_activity"
android:configChanges="orientation|screenSize|keyboardHidden" />
<activity
android:name=".MyActivity"
android:label="@string/title_my_activity"
android:screenOrientation="portrait" />
@noaht11
noaht11 / OC-SavingStateActivity.java
Last active August 29, 2015 14:27
How to save very simple state
private static final String STATE_COUNTER = "counter";
private int mCounter;
...
@Override
protected void onSaveInstanceState(Bundle outState) {
// Make sure to call the super method so that the states of our views are saved
super.onSaveInstanceState(outState);
@noaht11
noaht11 / OC-RestoringStateActivity.java
Last active August 29, 2015 14:27
Restoring very simple state
private static final String STATE_COUNTER = "counter";
private TextView mCounterTextView;
private int mCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
@noaht11
noaht11 / OC--SavingStateListViewActivity.java
Last active August 29, 2015 14:27
Saving state for a ListView
private static final String STATE_ITEMS = "items";
// Make sure to declare as ArrayList so it's Serializable
private ArrayList<Item> mItems;
...
@Override
protected void onSaveInstanceState(Bundle outState) {
// Make sure to call the super method so that the states of our views are saved
@noaht11
noaht11 / OC-RestoringStateFragment.java
Last active July 6, 2018 12:52
Where to restore state in a Fragment
private static final String STATE_ID = "id";
private static final String STATE_ITEMS = "items";
private long mId;
private ArrayList<Item> mItems;
private ListView mListView;
private ArrayAdapter<Item> mAdapter;
@Override
@noaht11
noaht11 / OC-AddingFragmentsActivity.java
Last active July 11, 2016 16:33
How to properly add a Fragment to an Activity so that you can handle orientation changes
private static final String TAG_MY_FRAGMENT = "myFragment";
private MyFragment mFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adding_fragments);
@noaht11
noaht11 / OC-AsyncTaskCancelActivity.java
Last active January 17, 2018 13:02
Cancelling an AsyncTask in onDestroy
private static final String STATE_TASK_RUNNING = "taskRunning";
private MyTask mTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
@noaht11
noaht11 / OC-RxUnsubscribeActivity.java
Last active August 8, 2016 04:32
Unsubscribing from an RxJava subscription to handle orientation changes
private Subscription mDoSomethingSubscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
// Setup views
...
@noaht11
noaht11 / OC-AsyncTaskRetainedFragment.java
Last active October 3, 2017 08:01
Using a retained fragment to handle AsyncTasks across orientation changes
public class NetworkRequestFragment extends Fragment {
// Declare some sort of interface that your AsyncTask will use to communicate with the Activity
public interface NetworkRequestListener {
void onRequestStarted();
void onRequestProgressUpdate(int progress);
void onRequestFinished(SomeObject result);
}
private NetworkTask mTask;