From 498f932a3fbc54f9301071de4a4e039d5780cb2d Mon Sep 17 00:00:00 2001 From: Kevin Meyer Date: Wed, 20 Aug 2014 15:46:55 +0200 Subject: [PATCH] reduced multiple code blocks by adding BaseActionBarActivity class. --- .../apps/Poche/BaseActionBarActivity.java | 37 +++++++++++++++++++ .../fr/gaulupeau/apps/Poche/ListArticles.java | 18 ++------- .../fr/gaulupeau/apps/Poche/ReadArticle.java | 23 +----------- .../fr/gaulupeau/apps/Poche/Settings.java | 21 +---------- 4 files changed, 43 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/fr/gaulupeau/apps/Poche/BaseActionBarActivity.java diff --git a/app/src/main/java/fr/gaulupeau/apps/Poche/BaseActionBarActivity.java b/app/src/main/java/fr/gaulupeau/apps/Poche/BaseActionBarActivity.java new file mode 100644 index 0000000..e04e598 --- /dev/null +++ b/app/src/main/java/fr/gaulupeau/apps/Poche/BaseActionBarActivity.java @@ -0,0 +1,37 @@ +package fr.gaulupeau.apps.Poche; + + +import android.annotation.TargetApi; +import android.app.Activity; +import android.os.Build; +import android.os.Bundle; +import android.view.MenuItem; + +public class BaseActionBarActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addBackButtonToActionBar(); + } + + @TargetApi(11) + protected void addBackButtonToActionBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + try { + getActionBar().setDisplayHomeAsUpEnabled(true); + } catch (Exception e) { + // + } + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + this.finish(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/fr/gaulupeau/apps/Poche/ListArticles.java b/app/src/main/java/fr/gaulupeau/apps/Poche/ListArticles.java index 409bc80..308690a 100644 --- a/app/src/main/java/fr/gaulupeau/apps/Poche/ListArticles.java +++ b/app/src/main/java/fr/gaulupeau/apps/Poche/ListArticles.java @@ -16,7 +16,7 @@ import android.view.View; import android.widget.AdapterView; import android.widget.ListView; -public class ListArticles extends Activity { +public class ListArticles extends BaseActionBarActivity { private ArrayList
readArticlesInfo; private ListView readList; @@ -25,15 +25,6 @@ public class ListArticles extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - try { - getActionBar().setDisplayHomeAsUpEnabled(true); - } catch (Exception e) { - // - } - } - setupDB(); setupList(false); } @@ -60,15 +51,12 @@ public class ListArticles extends Activity { switch (item.getItemId()) { case R.id.menuShowAll: setupList(true); - return super.onOptionsItemSelected(item); + return true; case R.id.menuWipeDb: ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this); helper.truncateTables(database); setupList(false); - return super.onOptionsItemSelected(item); - case android.R.id.home: - this.finish(); - return super.onOptionsItemSelected(item); + return true; default: return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/fr/gaulupeau/apps/Poche/ReadArticle.java b/app/src/main/java/fr/gaulupeau/apps/Poche/ReadArticle.java index 2c50fd9..6c42159 100644 --- a/app/src/main/java/fr/gaulupeau/apps/Poche/ReadArticle.java +++ b/app/src/main/java/fr/gaulupeau/apps/Poche/ReadArticle.java @@ -34,7 +34,7 @@ import java.net.URL; import fr.gaulupeau.apps.InThePoche.R; -public class ReadArticle extends Activity { +public class ReadArticle extends BaseActionBarActivity { WebView webViewContent; Button btnMarkRead; SQLiteDatabase database; @@ -42,19 +42,11 @@ public class ReadArticle extends Activity { ScrollView view; public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); requestWindowFeature(Window.FEATURE_PROGRESS); + super.onCreate(savedInstanceState); setContentView(R.layout.article); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - try { - getActionBar().setDisplayHomeAsUpEnabled(true); - } catch (Exception e) { - // - } - } - view = (ScrollView) findViewById(R.id.scroll); ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(getApplicationContext()); database = helper.getWritableDatabase(); @@ -149,15 +141,4 @@ public class ReadArticle extends Activity { super.onDestroy(); database.close(); } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - this.finish(); - return super.onOptionsItemSelected(item); - default: - return super.onOptionsItemSelected(item); - } - } } diff --git a/app/src/main/java/fr/gaulupeau/apps/Poche/Settings.java b/app/src/main/java/fr/gaulupeau/apps/Poche/Settings.java index 7cbd66b..38f1afc 100644 --- a/app/src/main/java/fr/gaulupeau/apps/Poche/Settings.java +++ b/app/src/main/java/fr/gaulupeau/apps/Poche/Settings.java @@ -13,7 +13,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -public class Settings extends Activity { +public class Settings extends BaseActionBarActivity { Button btnDone; EditText editPocheUrl; EditText editAPIUsername; @@ -26,14 +26,6 @@ public class Settings extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.settings); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - try { - getActionBar().setDisplayHomeAsUpEnabled(true); - } catch (Exception e) { - // - } - } - SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String pocheUrl = settings.getString("pocheUrl", "http://"); String apiUsername = settings.getString("APIUsername", ""); @@ -63,15 +55,4 @@ public class Settings extends Activity { // } } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - this.finish(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } }