Skip to content

Commit 183ed89

Browse files
committed
Refactor so that Activity level UI can be accessed by Controllers.
1 parent 46c6562 commit 183ed89

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.android.architecture.blueprints.todoapp;
2+
3+
import android.support.v7.app.ActionBar;
4+
5+
public interface ActionBarProvider {
6+
ActionBar getSupportActionBar();
7+
}

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/BaseController.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
package com.example.android.architecture.blueprints.todoapp;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.v4.widget.DrawerLayout;
5+
import android.support.v7.app.ActionBar;
6+
import android.view.View;
7+
38
import com.bluelinelabs.conductor.Controller;
49

510
public abstract class BaseController extends Controller {
611
private boolean mActive = false;
712

13+
protected ActionBar getActionBar() {
14+
ActionBarProvider actionBarProvider = ((ActionBarProvider)getActivity());
15+
return actionBarProvider != null ? actionBarProvider.getSupportActionBar() : null;
16+
}
17+
18+
protected DrawerLayout getDrawerLayout() {
19+
DrawerLayoutProvider actionBarProvider = ((DrawerLayoutProvider)getActivity());
20+
return actionBarProvider != null ? actionBarProvider.getDrawerLayout() : null;
21+
}
22+
23+
@Override
24+
protected void onAttach(@NonNull View view) {
25+
super.onAttach(view);
26+
// Default settings to reset Activity UI state to each time a new controller is loaded.
27+
ActionBar actionBar = getActionBar();
28+
actionBar.setTitle("");
29+
actionBar.setDisplayHomeAsUpEnabled(false);
30+
31+
DrawerLayout drawerLayout = getDrawerLayout();
32+
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
33+
}
34+
835
protected void setActive(boolean active) {
936
mActive = active;
1037
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.android.architecture.blueprints.todoapp;
2+
3+
import android.support.v4.widget.DrawerLayout;
4+
5+
public interface DrawerLayoutProvider {
6+
DrawerLayout getDrawerLayout();
7+
}

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/MainActivity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.support.design.widget.NavigationView;
2222
import android.support.v4.view.GravityCompat;
2323
import android.support.v4.widget.DrawerLayout;
24-
import android.support.v7.app.ActionBar;
2524
import android.support.v7.app.AppCompatActivity;
2625
import android.support.v7.widget.Toolbar;
2726
import android.view.MenuItem;
@@ -36,7 +35,7 @@
3635
/**
3736
* The activity for the app.
3837
*/
39-
public class MainActivity extends AppCompatActivity {
38+
public class MainActivity extends AppCompatActivity implements DrawerLayoutProvider, ActionBarProvider {
4039

4140
private ViewGroup mContainer;
4241
private DrawerLayout mDrawerLayout;
@@ -52,10 +51,6 @@ protected void onCreate(Bundle savedInstanceState) {
5251
// Set up the toolbar.
5352
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
5453
setSupportActionBar(toolbar);
55-
ActionBar ab = getSupportActionBar();
56-
ab.setTitle(R.string.statistics_title);
57-
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
58-
ab.setDisplayHomeAsUpEnabled(true);
5954

6055
// Set up the controller container.
6156
mContainer = (ViewGroup) findViewById(R.id.controller_container);
@@ -118,4 +113,9 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
118113
}
119114
});
120115
}
116+
117+
@Override
118+
public DrawerLayout getDrawerLayout() {
119+
return mDrawerLayout;
120+
}
121121
}

todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.example.android.architecture.blueprints.todoapp.statistics;
1818

1919
import android.support.annotation.NonNull;
20+
import android.support.v4.widget.DrawerLayout;
21+
import android.support.v7.app.ActionBar;
2022
import android.view.LayoutInflater;
2123
import android.view.View;
2224
import android.view.ViewGroup;
@@ -64,9 +66,23 @@ protected void onDestroyView(View view) {
6466

6567
// Controllers are kept in a retained fragment during configuration changes.
6668
// All Views must be set to null to prevent leaking the old Activity.
69+
// It also saves memory since the Views on the back stack are destroyed and recreated.
6770
mStatisticsTV = null;
6871
}
6972

73+
@Override
74+
protected void onAttach(@NonNull View view) {
75+
super.onAttach(view);
76+
// Configure Activity level UI.
77+
ActionBar actionBar = getActionBar();
78+
actionBar.setTitle(R.string.statistics_title);
79+
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
80+
actionBar.setDisplayHomeAsUpEnabled(true);
81+
82+
DrawerLayout drawerLayout = getDrawerLayout();
83+
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
84+
}
85+
7086
@Override
7187
public void setProgressIndicator(boolean active) {
7288
if (active) {

0 commit comments

Comments
 (0)