Это моя версия решения метода расширения, которое является лишь небольшим изменением принятого ответа, но имеет то преимущество, что возвращенный счет основан на подтвержденном удалении элемента из коллекции :
public static class ObservableCollectionExtensionMethods
{
/// <summary>
/// Extends ObservableCollection adding a RemoveAll method to remove elements based on a boolean condition function
/// </summary>
/// <typeparam name="T">The type contained by the collection</typeparam>
/// <param name="observableCollection">The ObservableCollection</param>
/// <param name="condition">A function that evaluates to true for elements that should be removed</param>
/// <returns>The number of elements removed</returns>
public static int RemoveAll<T>(this ObservableCollection<T> observableCollection, Func<T, bool> condition)
{
// Find all elements satisfying the condition, i.e. that will be removed
var toRemove = observableCollection
.Where(condition)
.ToList();
// Remove the elements from the original collection, using the Count method to iterate through the list,
// incrementing the count whenever there's a successful removal
return toRemove.Count(observableCollection.Remove);
}
}
вам нужно переопределить onSaveInstanceState в вашей активности A и сохранить значение вашего текста редактирования следующим образом:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(TEXT_VIEW_KEY, mTextView.getText());
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
и в вашей активности onCreate теперь проверьте, есть ли какие-либо данные, которые были ранее сохранены, а затем инициализируйте текст
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// recovering the instance state
if (savedInstanceState != null) {
String text = savedInstanceState.getString(TEXT_VIEW_KEY,);
}
setContentView(R.layout.main_activity);
// initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_view);
mTextView.setText(text);
}
Вы также можете проверить компоненты архитектуры для Android, особенно компоненты, для облегчения вашей работы nxt время
проверьте этот вопрос , вам не нужно сохранять текст каким-либо образом, просто поместите finish()
в конец второго действия