reduced multiple code blocks by adding BaseActionBarActivity class.

This commit is contained in:
Kevin Meyer 2014-08-20 15:46:55 +02:00
parent 8e11c74aa0
commit 498f932a3f
4 changed files with 43 additions and 56 deletions

View File

@ -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);
}
}

View File

@ -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<Article> 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);
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}