Skip to content

Commit ead2801

Browse files
committed
fixed the back button problem
1 parent bd14759 commit ead2801

2 files changed

Lines changed: 76 additions & 57 deletions

File tree

‎de.vogella.android.todos/src/de/vogella/android/todos/TodoEdit.java‎

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Intent;
5+
import android.database.Cursor;
56
import android.os.Bundle;
67
import android.view.View;
78
import android.widget.Button;
@@ -12,45 +13,86 @@ public class TodoEdit extends Activity {
1213
private EditText mTitleText;
1314
private EditText mBodyText;
1415
private Long mRowId;
16+
private TodoDbAdapter mDbHelper;
1517

1618
@Override
1719
protected void onCreate(Bundle bundle) {
1820
super.onCreate(bundle);
19-
21+
mDbHelper = new TodoDbAdapter(this);
22+
mDbHelper.open();
2023
setContentView(R.layout.todo_edit);
2124
mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
2225
mBodyText = (EditText) findViewById(R.id.todo_edit_description);
2326
Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
2427
mRowId = null;
2528
Bundle extras = getIntent().getExtras();
29+
mRowId = (bundle == null) ? null : (Long) bundle
30+
.getSerializable(TodoDbAdapter.KEY_ROWID);
2631
if (extras != null) {
27-
String title = extras.getString(TodoDbAdapter.KEY_SUMMARY);
28-
String body = extras.getString(TodoDbAdapter.KEY_DESCRIPTION);
29-
mRowId = extras.getLong(TodoDbAdapter.KEY_ROWID);
30-
31-
if (title != null) {
32-
mTitleText.setText(title);
33-
}
34-
if (body != null) {
35-
mBodyText.setText(body);
36-
}
32+
mRowId = extras.getLong(TodoDbAdapter.KEY_ROWID);
3733
}
34+
System.out.println("So far so good");
35+
populateFields();
3836
confirmButton.setOnClickListener(new View.OnClickListener() {
39-
40-
public void onClick(View view) {
41-
Bundle bundle = new Bundle();
42-
bundle.putString(TodoDbAdapter.KEY_SUMMARY, mTitleText.getText().toString());
43-
bundle.putString(TodoDbAdapter.KEY_DESCRIPTION, mBodyText.getText().toString());
44-
if (mRowId != null) {
45-
bundle.putLong(TodoDbAdapter.KEY_ROWID, mRowId);
46-
}
47-
48-
Intent mIntent = new Intent();
49-
mIntent.putExtras(bundle);
50-
setResult(RESULT_OK, mIntent);
51-
finish();
52-
}
53-
37+
public void onClick(View view) {
38+
Bundle bundle = new Bundle();
39+
bundle.putString(TodoDbAdapter.KEY_SUMMARY, mTitleText
40+
.getText().toString());
41+
bundle.putString(TodoDbAdapter.KEY_DESCRIPTION, mBodyText
42+
.getText().toString());
43+
if (mRowId != null) {
44+
bundle.putLong(TodoDbAdapter.KEY_ROWID, mRowId);
45+
}
46+
47+
Intent mIntent = new Intent();
48+
mIntent.putExtras(bundle);
49+
setResult(RESULT_OK, mIntent);
50+
finish();
51+
}
52+
5453
});
5554
}
55+
56+
private void populateFields() {
57+
if (mRowId != null) {
58+
Cursor todo = mDbHelper.fetchTodo(mRowId);
59+
startManagingCursor(todo);
60+
mTitleText.setText(todo.getString(todo
61+
.getColumnIndexOrThrow(TodoDbAdapter.KEY_SUMMARY)));
62+
mBodyText.setText(todo.getString(todo
63+
.getColumnIndexOrThrow(TodoDbAdapter.KEY_DESCRIPTION)));
64+
}
65+
}
66+
67+
protected void onSaveInstanceState(Bundle outState) {
68+
super.onSaveInstanceState(outState);
69+
saveState();
70+
outState.putSerializable(TodoDbAdapter.KEY_ROWID, mRowId);
71+
}
72+
73+
@Override
74+
protected void onPause() {
75+
super.onPause();
76+
saveState();
77+
}
78+
79+
@Override
80+
protected void onResume() {
81+
super.onResume();
82+
populateFields();
83+
}
84+
85+
private void saveState() {
86+
String summary = mTitleText.getText().toString();
87+
String description = mBodyText.getText().toString();
88+
89+
if (mRowId == null) {
90+
long id = mDbHelper.createTodo(summary, description);
91+
if (id > 0) {
92+
mRowId = id;
93+
}
94+
} else {
95+
mDbHelper.updateTodo(mRowId, summary, description);
96+
}
97+
}
5698
}

‎de.vogella.android.todos/src/de/vogella/android/todos/Todos.java‎

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import de.vogella.android.todos.database.TodoDbAdapter;
1717

1818
public class Todos extends ListActivity {
19-
private TodoDbAdapter mDbHelper;
19+
private TodoDbAdapter dbHelper;
2020
private static final int ACTIVITY_CREATE = 0;
2121
private static final int ACTIVITY_EDIT = 1;
2222
private static final int DELETE_ID = Menu.FIRST + 1;
@@ -27,9 +27,10 @@ public class Todos extends ListActivity {
2727
public void onCreate(Bundle savedInstanceState) {
2828
super.onCreate(savedInstanceState);
2929
setContentView(R.layout.todo_list);
30-
mDbHelper = new TodoDbAdapter(this);
31-
mDbHelper.open();
30+
dbHelper = new TodoDbAdapter(this);
31+
dbHelper.open();
3232
fillData();
33+
System.out.println("Hallo");
3334
registerForContextMenu(getListView());
3435
}
3536

@@ -68,7 +69,7 @@ public boolean onContextItemSelected(MenuItem item) {
6869
case DELETE_ID:
6970
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
7071
.getMenuInfo();
71-
mDbHelper.deleteTodo(info.id);
72+
dbHelper.deleteTodo(info.id);
7273
fillData();
7374
return true;
7475
}
@@ -84,15 +85,10 @@ private void createTodo() {
8485
@Override
8586
protected void onListItemClick(ListView l, View v, int position, long id) {
8687
super.onListItemClick(l, v, position, id);
87-
Cursor c = cursor;
88-
c.moveToPosition(position);
8988
Intent i = new Intent(this, TodoEdit.class);
9089
i.putExtra(TodoDbAdapter.KEY_ROWID, id);
91-
i.putExtra(TodoDbAdapter.KEY_SUMMARY,
92-
c.getString(c.getColumnIndexOrThrow(TodoDbAdapter.KEY_SUMMARY)));
93-
i.putExtra(TodoDbAdapter.KEY_DESCRIPTION, c.getString(c
94-
.getColumnIndexOrThrow(TodoDbAdapter.KEY_DESCRIPTION)));
9590
// Activity returns an result if called with startActivityForResult
91+
9692
startActivityForResult(i, ACTIVITY_EDIT);
9793
}
9894

@@ -104,31 +100,12 @@ protected void onListItemClick(ListView l, View v, int position, long id) {
104100
protected void onActivityResult(int requestCode, int resultCode,
105101
Intent intent) {
106102
super.onActivityResult(requestCode, resultCode, intent);
107-
Bundle extras = intent.getExtras();
108-
109-
switch (requestCode) {
110-
case ACTIVITY_CREATE:
111-
String title = extras.getString(TodoDbAdapter.KEY_SUMMARY);
112-
String body = extras.getString(TodoDbAdapter.KEY_DESCRIPTION);
113-
mDbHelper.createTodo(title, body);
114-
fillData();
115-
break;
116-
case ACTIVITY_EDIT:
117-
Long mRowId = extras.getLong(TodoDbAdapter.KEY_ROWID);
118-
if (mRowId != null) {
119-
String editTitle = extras.getString(TodoDbAdapter.KEY_SUMMARY);
120-
String editBody = extras
121-
.getString(TodoDbAdapter.KEY_DESCRIPTION);
122-
mDbHelper.updateTodo(mRowId, editTitle, editBody);
123-
}
124-
fillData();
125-
break;
126-
}
103+
fillData();
127104

128105
}
129106

130107
private void fillData() {
131-
cursor = mDbHelper.fetchAllTodos();
108+
cursor = dbHelper.fetchAllTodos();
132109
startManagingCursor(cursor);
133110

134111
String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };

0 commit comments

Comments
 (0)