Skip to content

Commit

Permalink
Fix rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
outadoc committed Apr 5, 2014
1 parent ddb580f commit 64054d1
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/fr/outadev/twistoast/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

drawerEntries = getResources().getStringArray(R.array.drawer_entries);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);

drawerTitle = getTitle();
actionBarTitle = drawerEntries[0];
setTitle(actionBarTitle);

frags = new Fragment[drawerEntries.length];

if(savedInstanceState != null) {
currentFragmentIndex = savedInstanceState.getInt("key_current_frag");
} else {
currentFragmentIndex = 0;
}

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.action_ok,
R.string.action_delete) {
Expand All @@ -48,19 +53,20 @@ public void onDrawerOpened(View drawerView) {
}
};

frags = new Fragment[drawerEntries.length];

drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerLayout.setDrawerListener(drawerToggle);

drawerList.setAdapter(new NavDrawerArrayAdapter(this, R.layout.drawer_list_item, drawerEntries));
drawerList.setItemChecked(0, true);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);


actionBarTitle = drawerEntries[currentFragmentIndex];
setTitle(actionBarTitle);

drawerList.setAdapter(new NavDrawerArrayAdapter(this, R.layout.drawer_list_item, drawerEntries, currentFragmentIndex));
drawerList.setItemChecked(currentFragmentIndex, true);

if(savedInstanceState == null) {
loadFragmentFromDrawerPosition(0);
loadFragmentFromDrawerPosition(currentFragmentIndex);
}
}

Expand Down Expand Up @@ -118,7 +124,7 @@ public void loadFragmentFromDrawerPosition(int position) {
public void onBackPressed() {
if(frags[currentFragmentIndex] instanceof WebViewFragment && ((WebViewFragment) frags[currentFragmentIndex]).canGoBack()) {
((WebViewFragment) frags[currentFragmentIndex]).goBack();
} else if(frags[currentFragmentIndex] instanceof StopsListFragment) {
} else if(currentFragmentIndex == 0) {
super.onBackPressed();
} else {
drawerLayout.openDrawer(Gravity.LEFT);
Expand All @@ -132,16 +138,6 @@ public void checkDrawerItem(int position) {
drawerList.setItemChecked(position, true);
}

@Override
protected void onResume() {
super.onResume();
}

@Override
protected void onPause() {
super.onPause();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Expand All @@ -163,6 +159,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("key_current_frag", currentFragmentIndex);
}

private String[] drawerEntries;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
Expand Down

0 comments on commit 64054d1

Please sign in to comment.