I have an Android Activity called Activity A shown below. When I click on the saveButton, the saveData() method is called and then the finish() method gets called and closes Activity A. When I click on the triggerActivity_B button in Activity A to go to Activity B and then click on the triggerActivity_A button in Activity B to go back to Activity A, I click on the saveButton and the saveData() method doesn't get called, just the finish() method gets called. It only seems to execute when I don't leave the Activity, I'm not sure why this is.
Your help would be most appreciated.
ActivityA.java:
public class ActivityA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveData();
开发者_如何学编程 finish();
}
});
triggerActivity_B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(ActivityA.this, ActivityB.class);
startActivity(i);
finish();
}
});
}
private void saveData() {
String desc = descriptionEntry.getText().toString();
if (mRowId == null) {
long id = mDbHelper2.createDescription(desc);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper2.updateDescription(mRowId, desc);
}
}
}
ActivityB.java:
public class ActivityB extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
triggerActivity_A.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(ActivityB.this, ActivityA.class);
startActivity(i);
finish();
}
});
}
}
Point is if you click on the triggerActivity_A button in ActivityB, a new instance of ActivityA is being made; it doesn't go back to your first ActivityA and thus probably has nothing to save.
Also note that your first activityA seizes to exist when you push the button to open ActivityB, as you are calling finish(). If you want to keep the first ActivityA don't call finish(), and don't start a new activity when pressing the button in ActivityB, just call finish() only).
There is no reason to assume saveData() isn't called.
And do please improve your acceptrate.
精彩评论