我的场景很简单:我有一个MainActivity,它调用列出所有类别的CategoryActivity。在这个CategoryActivity中,我有一个添加新类别的按钮。当用户单击此按钮时,将调用RegisterCategoryActivity,用户填写字段然后保存。保存之后,我在RegisterCategoryActivity上调用了finish(),但问题是如果我再次尝试添加一个新类别,则不再调用RegisterCategoryActivity但是如果我回到我的MainActivity - >单击CategoryActivity然后单击按钮,它可以正常工作。我在这里缺少什么?
CategoryActivity
public class CategoryActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
}
@Override
public void onResume() {
new LoadCategories().execute(); // load categories from DB
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_activity_category, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_activity_category:
Intent registerCategoryActivity = new Intent(CategoryActivity.this, RegisterCategoryActivity.class);
registerCategoryActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(registerCategoryActivity);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
RegisterCategoryActivity
public class RegisterCategoryActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_category);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_save, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
validateFields();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Inner class for implementing progress bar before fetching data **/
private class RegisterCategory extends AsyncTask<Void, Void, Integer>
{
@Override
protected void onPreExecute()
{
...
}
@Override
protected Integer doInBackground(Void... params)
{
int result;
...
return result;
}
@Override
protected void onPostExecute(Integer result)
{
...
finish();
}
}
}
的AndroidManifest.xml
<activity
android:name="com.app.RegisterCategoryActivity"
android:icon="@drawable/ic_categories"
android:label="@string/registerCategory"
android:windowSoftInputMode="stateHidden">
</activity>
<activity
android:name="com.app.CategoryActivity"
android:icon="@drawable/ic_categories"
android:label="@string/categories">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>