Ngay cả sau khi xóa thủ công Int ent và Intent extras sau khi chúng được phân tích cú pháp, có vẻ như Activity.getIntent() sẽ luôn trả về Intent ban đầu đã bắt đầu Activity.
Để làm được việc này, tôi khuyên bạn nên một cái gì đó như thế này:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The Intent provided by getIntent() (and its extras) will persist through a restore
// via savedInstance. Because of this, restoring this activity from a
// an instance that was originally started with extras (deep-link or
// pre-defined destination) may cause un-desired behavior
// (ie...infinite loop of sending the user directly to somewhere else because of a
// pre-defined alternate destination in the Intent's extras).
//
// To get around this, if restoring from savedInstanceState, we explicitly
// set a new Intent *** to override the original Intent that started the activity.***
// Note...it is still possible to re-use the original Intent values...simply
// set them in the savedInstanceState Bundle in onSavedInstanceState.
if (savedInstanceState != null) {
// Place savedInstanceState Bundle as the Intent "extras"
setIntent(new Intent().putExtras(savedInstanceState));
}
processIntent(getIntent())
}
private void processIntent(Intent intent) {
if (getIntent().getExtras() == null) {
// Protection condition
return;
}
doSomething(intent.getExtras.getString("SOMETHING_I_REALLY_NEED_TO_PERSIST"));
final String somethingIDontWantToPersist =
intent.getExtras.getString("SOMETHING_I_DONT_WANT_TO_PERSIST");
if(somethingIDontWantToPersist != null) {
doSomething(somethingIDontWantToPersist);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save selective extras from original Intent...
savedInstanceState.putString("SOMETHING_I_REALLY_NEED_TO_PERSIST", "persistedValued");
super.onSaveInstanceState(savedInstanceState);
}
Bằng cách này, có một cơ chế để đổ tiếp cận mục đích ban đầu trong khi vẫn duy trì khả năng giữ lại một cách rõ ràng một số phần của tiếp cận mục đích ban đầu/Ý định tính năng bổ sung.
Lưu ý rằng tôi chưa thử nghiệm tất cả chế độ Khởi chạy hoạt động.
pixel sẽ không chấp nhận câu trả lời sai rõ ràng và chấp nhận @ dcg thay thế. Điều đó đã cứu tôi khoảng một giờ làm việc đi xuống con đường sai lầm. Xem bài viết của Dianne Hackborn về cách setIntent là một lời nói dối: https://groups.google.com/forum/#!msg/android-developers/vrLdM5mKeoY/ThkOonAbtloJ – JohnnyLambada
Xem việc này có hữu ích không - stackoverflow.com/a/19820057/313042 – rajath